Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sorting on the gridview

4 views
Skip to first unread message

JohnE

unread,
Aug 18, 2009, 12:40:01 AM8/18/09
to
I have a datagrid on a page with 6 pages but having a sorting problem. It
does sort when the column header is selected. But, when I go to another page
by using the numbers in the bottom left corner of the grid, the sorting jumps
back to the original order. Below is the code that is being used in C#.

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

Ashish Sheth

unread,
Aug 18, 2009, 8:15:59 AM8/18/09
to
Hi 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...

JohnE

unread,
Aug 18, 2009, 9:18:02 AM8/18/09
to
Ashish, thanks for the reply. But were did sortExpression come from? The
red underlining is there and sortDirection.value as well. No build allowed.

Thanks... embarrassed newbie (john)

Ashish Sheth

unread,
Aug 19, 2009, 5:57:23 AM8/19/09
to
Sorry for confusion John.
sortExpression and sortDirection are the hiddenfield that you have to add to
your aspx page.
Basically you need to store the state of your sorting in some way. Here I am
storing the values in the hidden field but you can use viewstate also
instead of hiddenfield.

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...

0 new messages