How to Add Sorting to a GridView
Add Sorting to a Non-Paged GridView
Follow these instructions to add sorting to a GridView that is not set up for paging.
- Bind the GridView to a DataView rather than a DataTable. The easiest way to do this is to set the BindingSourceMember property of the GridView to the DefaultView of a DataTable. For example:

For details on binding to DataViews, see the Help topic Data Binding Web Forms Data Grids.
- Select the GridView in design mode and set its AllowSorting property to true.
- To specify the columns on which you want to allow end users to sort, select the GridView in design mode, click it's smart tag and select Edit Columns... from the shortcut menu which launches the Fields dialog.
Next, in the Selected fields list select the column you want to be sortable, and in the property list on the right side of the dialog, set the column's SortExpression property to the expression you want to sort on. Typically this is the same as the DataField property. For example:

Set the SortExpression property on each column you want to be sortable, then click OK to close the Fields dialog. Your GridView should now display the sortable column header text as hyperlinks that can be clicked to change the sort order of the GridView:

- Now you need to create an event handler for the GridView's Sorting event. To do this, select the GridView in design mode, then go to the Properties Window and click the Events button (the button with the lightning bolt). Double click the Sorting event to create an event handler.
- In the new event handler method, add code that retrieves the DataTable to which the GridView is bound, and sets its associated DefaultView.SortOrder property based on the currently selected column header.
For example, in C#:
protected void grdOrders_Sorting(object sender, GridViewSortEventArgs e) { // Get the sort direction from the event args string SortOrder = e.SortDirection == SortDirection.Ascending ? "ASC" : "DESC"; // Get the SortExpression from the event args and use it along with the sort order to // set the DefaultView's Sort property DataTable dtOrders = (DataTable)Session["dtOrders"]; dtOrders.DefaultView.Sort = e.SortExpression + " " + SortOrder; // Save the DataTable back to the session, then rebind the GridView Session["dtOrders"] = dtOrders; this.grdOrders.DataSource = dtOrders.DefaultView; this.grdOrders.DataBind(); }And in VB .NET:
Protected Sub grdOrders_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles grdOrders.Sorting '' Get the sort direction from the event args Dim SortOrder As String = IIf(e.SortDirection = SortDirection.Ascending, "ASC", "DESC") '' Get the SortExpression from the event args and use it along with the sort order to '' set the DefaultView's Sort property Dim dtOrders As DataTable = CType(Session("dtOrders"), DataTable) dtOrders.DefaultView.Sort = e.SortExpression + " " + SortOrder '' Save the DataTable back to the session, then rebind the GridView Session("dtOrders") = dtOrders Me.grdOrders.DataSource = dtOrders.DefaultView Me.grdOrders.DataBind() End Sub
© (c) 2026 Oak Leaf Enterprises, Inc., 1996-2026 • Updated: 04/26/18
Comment or report problem with topic
