private string ConvertSortDirectionToSQL(SortDirection sortDirection)
{
string newSortDirection = string.Empty;
switch(sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "ASC";
break;
case SortDirection.Descending:
newSortDirection = "DESC";
break;
}
return newSortDirection;
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dt = GridView1.DataSource as DataTable;
if (dt != null)
{
DataView dvw = new DataView(dt);
dvw.Sort = e.SortExpression + " " +
ConvertSortDirectionToSQL(e.SortDirection);
GridView1.DataSource = dvw;
GridView1.DataBind();
}
}
Can someone see the error(s) of my way for sorting and fill me in. I can
see why it is not holding the sort when changing pages.
I am new to ASP.Net, so please be kind.
... John
you need to save the sorting values in a hidden field or viewstate and next
time when you load the grid you need to read the sorting values from the
hidden field. Say your hidden field names are sortExpression and
sortDirection then your sorting method will look like below to save the
sorting values. After saving the values in the sorting method, when you
change the page, you need to handle the PageIndexChanged event and in that
you need to bind the grid again with the sorting values from the hidden
field.
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dt = GridView1.DataSource as DataTable;
if (dt != null)
{
DataView dvw = new DataView(dt);
dvw.Sort = e.SortExpression + " " +
ConvertSortDirectionToSQL(e.SortDirection);
sortExpression.value = e.SortExpression.ToString();
sortDirection.value =
ConvertSortDirectionToSQL(e.SortDirection);
GridView1.DataSource = dvw;
GridView1.DataBind();
}
}
Here is the PageIndexChanged event handler:
protected void GridView1_PageIndexChanged(object sender,
GridViewSortEventArgs e)
{
DataTable dt = GridView1.DataSource as DataTable;
if (dt != null)
{
DataView dvw = new DataView(dt);
if(!string.IsNullOrEmpty(sortExpression))
{
dvw.Sort = sortExpression.value + " " +
sortDirection.value;
}
GridView1.DataSource = dvw;
GridView1.DataBind();
}
}
hope this helps.
regards,
Ashish Sheth
"JohnE" <Jo...@discussions.microsoft.com> wrote in message
news:5BCA61FC-ADF5-4737...@microsoft.com...
Thanks... embarrassed newbie (john)
thanks and regards,
Ashish Sheth
BTW you don't have to be embarrased :)
"JohnE" <Jo...@discussions.microsoft.com> wrote in message
news:DCDCE74A-E0FB-4B5E...@microsoft.com...