Working with ASP.Net GridView sorting and paging

21,283

Check out this post sorting and paging with gridview asp.net

Basically you need to add server side event handlers for sorting and paging.

Here is an example - you can copy/paste most of it.

http://www.dotnetspider.com/resources/1249-Grid-View-Paging-Sorting.aspx

Share:
21,283
Emad-ud-deen
Author by

Emad-ud-deen

Updated on December 12, 2020

Comments

  • Emad-ud-deen
    Emad-ud-deen over 3 years

    We set up an ASP.Net GridView and included sorting and paging. When the user clicks on the GridView column header links for sorting the data or if the user clicks on the numbers links at the bottom of the GridView for paging the data, nothing happens.

    Here is a cut down version of the markup for the GridView:

    <asp:UpdatePanel 
        ID="UpdatePanelSummary" 
        runat="server" 
        UpdateMode="Always">
    
        <ContentTemplate> 
    
            <h1>Maintenance</h1>
    
            <% '-- GridView (Grid) for summary.                                                      -- %>
            <% '-- The user chooses a summary row from here and details are shown in a DetailsView.  -- %>
            <% '--------------------------------------------------------------------------------------- %>
    
            <asp:GridView
                ID="GridViewSummary" 
                runat="server" 
                AllowSorting="True" 
                AutoGenerateColumns="False" 
                DataKeyNames="ID" 
                Width="224px" 
                AllowPaging="True" 
                PageSize="7">
    
                <Columns>
                    <asp:BoundField DataField="Unit" HeaderText="Unit" 
                        SortExpression="Unit" />
    
                    <asp:BoundField DataField="TheName" HeaderText="Name" 
                        SortExpression="TheName" />
    
                    <asp:BoundField DataField="ID" 
                        HeaderText="ID" SortExpression="ID" InsertVisible="False" ReadOnly="True" 
                        Visible="False" />
    
                    <asp:CommandField ButtonType="Button" SelectText="Select Unit Details" 
                        ShowSelectButton="True" />
                </Columns>
            </asp:GridView>
        </ContentTemplate>
    </asp:UpdatePanel>
    

    Since nothing is happening, we are assuming we need to write some coding in the code-behind file. Can you show us what coding is needed to wake up the sorting and paging?

  • Emad-ud-deen
    Emad-ud-deen over 11 years
    Thanks for the quick reply. I will look at it.
  • Emad-ud-deen
    Emad-ud-deen over 11 years
    Thanks for the addition. I will try to convert it to VB.Net and let you know if we can get it working.
  • Emad-ud-deen
    Emad-ud-deen over 11 years
    Got the paging working. Now I will work on the sorting. Thanks again. :-)