ASP.NET GridView sorting: I want to change the sorting direction from ascending to descending when I click the column name. But by default, sorting direction always showing ascending. Am I missing any property setting to toggle sorting direction? Any help greatly appreciated. Thanks.
protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = myDataTable(strSQL);
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " + SortDirection(e.SortDirection);
gridView.DataSource = dataView;
gridView.DataBind();
}
}
//e.SortDirection always showing ascending
aspx gridview column design for sorting:
<asp:BoundField DataField="CREATE_DATE" HeaderText="Create Date" SortExpression="CREATE_DATE" />
Did you try setting the
AllowSortingattribute of theGridViewto true in the markup?Or, you could do it in your code behind after the DataBind:
This attribute allows the user to click on the column headers to sort on that column.
Edit: You actually have to create logic to decide whether to sort ascending or descending (
e.SortDirectionwill only help you if your using a “data source control” likeSQLDataSource). Then you can just change your dataView.Sort code to some like this:Where
someConditionis your logic for deciding which way to sort (for instance you could keep track of what the last sort direction was for that column, then do the opposite if the user clicks it again).