Skip to main content
CSharpUniversity ASP.NET and C# lessons

The search runs entirely in your browser over the published lesson list.

Letting the visitor sort the grid

Sorting by column heading is a small feature that makes a long list far more usable.

Turn it on

Markup
<asp:GridView id="productsGrid" runat="server"
    DataSourceID="productsSource"
    AllowSorting="true" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Name"
            HeaderText="Product" SortExpression="Name" />
    </Columns>
</asp:GridView>

What has to be true

  • Each sortable column needs a sort expression that names a real column in the query.
  • The data source must be able to sort, which the data source control handles for you.
Sort before you page. When a grid both sorts and pages, the sort must be applied to the whole result, not just the page in view, or the second page will be out of order.

Paging is the natural companion, in paging through many rows.

New lessons by email

Leave your address and get a short note whenever a fresh lesson is published. No account needed and you can stop at any time.

Thank you. Your address has been noted in this browser session. Connect a mailing service to deliver the notes for real.