Home All Groups Group Topic Archive Search About

Gridview custom sort - works but always sorts ascending

Author
15 Mar 2006 10:09 PM
David Adams
Hi,

I have a gridview in which I am doing a custom sort for the lastname of a
person.  When that column is clicked, I would like to additionally sort the
firstname ascending as such:

protected void gvClients_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (e.SortExpression == "szLastName")
        {
            e.SortExpression +=", szFirstName";
        }
  }

This works, but e.SortDirection is always Ascending.  How can I get this to
sort descending?

Thanks,
David

Author
16 Mar 2006 1:34 PM
CaffieneRush@gmail.com
If you want the firstName sort direction to match lastName then you'll
need to test for SortDirection and change the SortExpression
accordingly.
I haven't tested code below but it should be something like:
if (e.SortDirection == SortDirection.Ascending && e.SortExpression
=="szLastName")
{
  e.SortExpression += ", szFirstName";
}
else
{
  e.SortExpression += ", szFirstName DESC";
}
Author
16 Mar 2006 9:05 PM
David Adams
Thanks for your response but I don't think I was clear enough.  The problem
is not that I want to change the sort order of szFirstName, but that
e.SortDirection is always Ascending when I modify e.SortExpression.  If I
comment this out, e.SortDirection alternates between ASC and DESC as it
should.


<CaffieneR***@gmail.com> wrote in message
Show quoteHide quote
news:1142516071.889893.220690@p10g2000cwp.googlegroups.com...
> If you want the firstName sort direction to match lastName then you'll
> need to test for SortDirection and change the SortExpression
> accordingly.
> I haven't tested code below but it should be something like:
> if (e.SortDirection == SortDirection.Ascending && e.SortExpression
> =="szLastName")
> {
>  e.SortExpression += ", szFirstName";
> }
> else
> {
>  e.SortExpression += ", szFirstName DESC";
> }
>
Author
16 Mar 2006 10:48 PM
CaffieneRush@gmail.com
I see, so you're saying that the sort direction is no longer
automatically toggling after modifying the SortExpression. That means
you'll need to do it yourself.

if (e.SortDirection == SortDirection.Ascending &&
e.SortExpression.StartsWith("szLastName"))
{
  e.SortExpression = " szLastName ASC, szFirstName ASC";
  'Manually toggle SortDirection;
  e.SortDirection = SortDirection.Descending;
}
else if (e.SortDirection == SortDirection.Descending &&
e.SortExpression.StartsWith("szLastName"))
{
  e.SortExpression = " szLastName DESC, szFirstName DESC";
  'Manually toggle SortDirection;
  e.SortDirection = SortDirection.Ascending;
}

Good Luck.