NickMalloy
asked on
GridView Sorting not working properly
My gridview is not sorting Desc. It always goes to Asc. I fill the Grid using a DataTable. here is my sort function. Am I missing something?
private string grdSortDirection(SortDirection sortDirection)
{
string newSortDirection = String.Empty;
switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "DESC";
break;
case SortDirection.Descending:
newSortDirection = "ASC";
break;
}
return newSortDirection;
}
protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = grdOld.DataSource as DataTable;
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " + grdSortDirection(e.SortDirection);
grdOld.DataSource = dataView;
grdOld.DataBind();
}
}
ASKER
How do you set it?
replace the code, and remove your function.
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
string newSortDirection;
switch (e.SortDirection)
{
case SortDirection.Ascending:
newSortDirection = "DESC"; e.SortDirection = SortDirection.Descending;
break;
case SortDirection.Descending:
newSortDirection = "ASC"; e.SortDirection = SortDirection.Ascending;
break;
}
dataView.Sort = e.SortExpression + " " + newSortDirection;
grdOld.DataSource = dataView;
grdOld.DataBind();
}
ASKER
I changed my sort function to what you had, and now It sorts DESC and that is it
protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = grdOld.DataSource as DataTable;
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
string newSortDirection = "";
switch (e.SortDirection)
{
case SortDirection.Ascending:
newSortDirection = "DESC"; e.SortDirection = SortDirection.Descending;
break;
case SortDirection.Descending:
newSortDirection = "ASC"; e.SortDirection = SortDirection.Ascending;
break;
}
dataView.Sort = e.SortExpression + " " + newSortDirection;
grdOld.DataSource = dataView;
grdOld.DataBind();
}
}
protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = grdOld.DataSource as DataTable;
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
string newSortDirection = "";
switch (e.SortDirection)
{
case SortDirection.Ascending:
newSortDirection = "DESC"; e.SortDirection = SortDirection.Descending;
break;
case SortDirection.Descending:
newSortDirection = "ASC"; e.SortDirection = SortDirection.Ascending;
break;
}
dataView.Sort = e.SortExpression + " " + newSortDirection;
grdOld.DataSource = dataView;
grdOld.DataBind();
}
}
Did you put a break point inside your method?
Your dataTable has value?
Your dataTable has value?
ASKER
yes, everytime it goes through the case SortDirection.Ascending statement.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You forgot to Set the "e.SortDirection" after change it.