Wednesday, 20 July 2011

Sorting and Paging a GridView with ASP.NET 4.0


This tutorial will demonstrate how to use sorting and paging with a GridView control to make it more user friendly using ASP.NET 4.0.

Adding the Database
To demonstrate how to use sorting and paging with a grid view, we will need a sample database to use to populate our grid view with data. In this tutorial I will be using the ever so popular Northwind sample database that is available for download from Microsoft. You can locate the latest version here. At this point, I have created a new ASP.NET Empty Web Site and am ready to add the Northwind database to it. To do this:
  1. Right click the project in your solution explorer.
  2. Select add ASP.NET folder.
  3. Select App_Data.
  4. Right click the App_Data folder.
  5. Select add existing item...
  6. Select the Northwind.mdf database file.
  7. Click add.
We used over 10 web hosting companies before we found Server Intellect. Our new server with cloud hosting,was set up in less than 24 hours. We were able to confirm our order over the phone. They responded to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

Databinding the Gridview
Next, we need to bind the database we've added to a grid view. In order to do this, we will need to make a simple web page. To do this:
  1. Right click the project in your solution explorer.
  2. Select add new item...
  3. Select a web form.
  4. Name it 'Default.aspx'.
  5. Open Default.aspx up to design mode.
  6. Drag and drop a Gridview onto the web form.
  7. Expand the Gridview Tasks menu.
  8. From the choose data source drop down list, choose <New data source...>
  9. Select a SQL Database and name it 'SqlDataSource1'.
  10. Click ok.
  11. For the data connection, choose the Northwind.mdf database.
  12. Click next.
  13. Choose to save the connection string and name it 'ConnectionString'.
  14. Click next.
  15. Leave the default SQL statement that selects all from the Alphabetical list of products.
  16. Click next.
  17. Test the query and click Finish.
If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

Enabling Sorting and Paging
Now that we have our grid view displaying data from the database, we can enable sorting and paging. First, let's load up the web site and take a look at what the default grid view is displaying. You'll notice that when you load up the page all of the data is in one big unsorted list that is not very user friendly. By default, the grid view control allows us to add in sorting and paging very easily. To do this:
  1. Open Default.aspx up to design mode.
  2. Expand the Gridview Tasks menu.
  3. Check enable paging.
  4. Check enable sorting.
This will allow us to sort by different columns and also display only a certain amount of results on each page.

I just signed up at Server Intellect and couldn't be more pleased with my fully scalable & redundant cloud hosting! Check it out and see for yourself.

Testing
To test this out, load up the web site. Click on the Product Name column to sort by the product's name. Notice that only 10 items are being displayed and that there are links on the bottom to jump to different pages. The grid view allows us to implement some great functionality by simply checking a few options which we can then customize to fit our needs.  

The Default.aspx source looks like this:
<body>
    <form id="form1" runat="server">
    <div>

        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ProductID" 
            DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="ProductID" HeaderText="ProductID" 
                    InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
                <asp:BoundField DataField="ProductName" HeaderText="ProductName" 
                    SortExpression="ProductName" />
                <asp:BoundField DataField="SupplierID" HeaderText="SupplierID" 
                    SortExpression="SupplierID" />
                <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" 
                    SortExpression="CategoryID" />
                <asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit" 
                    SortExpression="QuantityPerUnit" />
                <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" 
                    SortExpression="UnitPrice" />
                <asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" 
                    SortExpression="UnitsInStock" />
                <asp:BoundField DataField="UnitsOnOrder" HeaderText="UnitsOnOrder" 
                    SortExpression="UnitsOnOrder" />
                <asp:BoundField DataField="ReorderLevel" HeaderText="ReorderLevel" 
                    SortExpression="ReorderLevel" />
                <asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued" 
                    SortExpression="Discontinued" />
                <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" 
                    SortExpression="CategoryName" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
            SelectCommand="SELECT * FROM [Alphabetical list of products]">
        </asp:SqlDataSource>

    </div>
    </form>
</body>

No comments:

Post a Comment