Hi
It is going in SortGridview & SortExpression/Direction value also gets changed but it is not sorting the data.
private void SortGridView(string sortExpression, string direction)
{
// You can cache the DataTable for improving performance
GetData();
DataTable dt = grdPlanning.DataSource as DataTable;
DataView dv = new DataView(dt);
dv.Sort = sortExpression + direction;
grdPlanning.DataSource = dv;
grdPlanning.DataBind();
}
protected void grdPlanning_Sorting(object sender, GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression;
if (GridViewSortDirection == SortDirection.Ascending)
{
GridViewSortDirection = SortDirection.Descending;
SortGridView(sortExpression, DESCENDING);
}
else
{
GridViewSortDirection = SortDirection.Ascending;
SortGridView(sortExpression, ASCENDING);
}
}
private const string ASCENDING = " ASC";
private const string DESCENDING = " DESC";
public SortDirection GridViewSortDirection
{
get
{
if (ViewState["sortDirection"] == null)
ViewState["sortDirection"] = SortDirection.Ascending;
return (SortDirection)ViewState["sortDirection"];
}
set
{
ViewState["sortDirection"] = value;
}
}
Jay PankhaniyaPosted May 1, 2023, 1:23 PM
Based on the code you provided, it seems that you are trying to sort a GridView in ASP.NET Web Forms by binding it to a DataView that is sorted based on the specified sort expression and direction.
However, you mentioned that the data is not getting sorted. There could be several reasons why this is happening, including:
AllowSortingproperty is not set totrueDataSourceproperty is not being set to the sorted DataView after callingSortGridViewHere are some possible solutions to these issues:
1. Make sure the
AllowSortingproperty of the GridView is set totruein the markup or code-behind2. Set the DataSource property of the GridView to the sorted DataView after calling SortGridView:
3, Check if the data in the DataTable is being sorted correctly by the DataView. You can do this by debugging the SortGridView method and inspecting the dv variable after setting the sort expression and direction.
Make sure that the sort expression is a valid column name in the DataTable and that the direction is either "ASC" or "DESC".