In this article, we shall see how to sort columns data in the GridView.
GridView control has a property called "AllowSorting" which enables us to sort the GridView records. So to enable sorting for GridView, set AllowSorting property of the GridView to true and you will need to also handle the OnSorting event. Setting AllowSorting to true converts the column headers to a clickable link and clicking those headers fires the OnSorting event.
To get hundreds of .NET How to’s like this, click here.
The following code snippet from ASPX page shows how these properties and events are set.
<asp:GridView ID="GridView1" OnSorting="SortRecords" runat="server"
AllowSorting="True" CellPadding="4" DataKeyNames="AutoId" />
string _connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
protected void SortRecords(object sender, GridViewSortEventArgs e)
string sortExpression = e.SortExpression;
string direction = string.Empty;
if (SortDirection == SortDirection.Ascending)
SortDirection = SortDirection.Descending;
direction = " DESC";
SortDirection = SortDirection.Ascending;
direction = " ASC";
DataTable table = this.GetData();
table.DefaultView.Sort = sortExpression + direction;
GridView1.DataSource = table;
private void BindData()
// specify the data source for the GridView
GridView1.DataSource = this.GetData();
// bind the data now
/// Gets or sets the grid view sort direction.
/// The grid view sort direction.
public SortDirection SortDirection
if (ViewState["SortDirection"] == null)
ViewState["SortDirection"] = SortDirection.Ascending;
ViewState["SortDirection"] = value;
private DataTable GetData()
DataTable table = new DataTable();
// get the connection
using (SqlConnection conn = new SqlConnection(_connStr))
// write the sql statement to execute
string sql = "SELECT AutoId, FirstName, LastName, Age, Active FROM PersonalDetail ORDER By AutoId";
// instantiate the command object to fire
using (SqlCommand cmd = new SqlCommand(sql, conn))
// get the adapter object and attach the command object to it
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
// fire Fill method to fetch the data and fill into DataTable
In the above code snippet, first we have populated records to the GridView (using BindData method) in the Page_Load event (inside Not IsPostBack, to ensure that data binding happens only if the request is not a postback request).
You may notice that on the .aspx page, we have specified OnSorting event (that fires SortRecords methods) and AllowSorting=true to the GridView. In this case we have set AutoGenerateColumns=true to the GridView, so our GridView columns are automatically generated .In case we are using BoundField or TemplateField, we can use SortExpression property to set the field name to sort on.
When any column of the GridView is clicked, the SortRecords server side method fires that first saves the sort expression and direction (Ascending or Descending order) into the string variable. We have a SortDirection property that saves the current sort direction in the ViewState and based on that the direction string variable is set as ASC or DESC. As the DataSource of this GridView is DataTable so we have used Sort method of its view and set the sort expression. This will sort the rows of the DataTable. After sorting, the GridView data source has been set and the DataBind method has been called.
Now when we run this page, we get the output as shown below.
Setting AllowSorting=true and handling the OnSorting event, we can sort the records of the GridView.