Home All Groups Group Topic Archive Search About

How to specify the sort column of a GridView

Author
28 Jun 2006 7:50 PM
ad
I use a GridView to display data ,  the datasource of my GridView is from a
ObjectDataSource.
How can I specify the sort column?

Author
28 Jun 2006 9:55 PM
Cowboy (Gregory A. Beamer) - MVP
1. Click on the grid in design view
2. Add an event handler for sort
    a) Click the events
    b) Add event for SortCommand
        (generally {gridname}_Sort)
3. In this event you will capture the SortExpression from e (event args)
4. Use this to create a DataView with an ASC or DESC filter

Example
private void grdReports_Sort(object source, DataGridSortCommandEventArgs e)
{
  string sortExpression = Session["QueueSortExpression"].ToString();
  string sortDirection = Session["QueueSortDirection"].ToString();

  if (sortExpression != e.SortExpression)
  {
    sortExpression = e.SortExpression;
    sortDirection = "asc";
  }
  else
  {
    if (sortDirection == "asc")
      sortDirection = "desc";
    else
      sortDirection = "asc";
  }

  Session["QueueSortExpression"] = sortExpression;
  Session["QueueSortDirection"] = sortDirection;

  DataView dv = (DataView) Session["sortedDV"];

  StringBuilder builder = new StringBuilder();
  builder.Append(sortExpression);
  builder.Append(" ");
  builder.Append(sortDirection);

  dv.Sort = builder.ToString();

  //Bind
  BindForm(null);
}



--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************


Show quote
"ad" wrote:

> I use a GridView to display data ,  the datasource of my GridView is from a
> ObjectDataSource.
> How can I specify the sort column?
>
>
>
Author
29 Jun 2006 3:58 AM
ad
Thanks,
But I do'nt how to add event handler for sort (Setp2)
Could you tell me more detail?


Show quote
"Cowboy (Gregory A. Beamer) - MVP" <NoSpamMgbworld@comcast.netNoSpamM> ¼¶¼g©ó¶l¥ó·s»D:9488134F-288B-48E1-B2CE-4DDC6B0B4***@microsoft.com...
> 1. Click on the grid in design view
> 2. Add an event handler for sort
>    a) Click the events
>    b) Add event for SortCommand
>        (generally {gridname}_Sort)
> 3. In this event you will capture the SortExpression from e (event args)
> 4. Use this to create a DataView with an ASC or DESC filter
>
> Example
> private void grdReports_Sort(object source, DataGridSortCommandEventArgs
> e)
> {
>  string sortExpression = Session["QueueSortExpression"].ToString();
>  string sortDirection = Session["QueueSortDirection"].ToString();
>
>  if (sortExpression != e.SortExpression)
>  {
>    sortExpression = e.SortExpression;
>    sortDirection = "asc";
>  }
>  else
>  {
>    if (sortDirection == "asc")
>      sortDirection = "desc";
>    else
>      sortDirection = "asc";
>  }
>
>  Session["QueueSortExpression"] = sortExpression;
>  Session["QueueSortDirection"] = sortDirection;
>
>  DataView dv = (DataView) Session["sortedDV"];
>
>  StringBuilder builder = new StringBuilder();
>  builder.Append(sortExpression);
>  builder.Append(" ");
>  builder.Append(sortDirection);
>
>  dv.Sort = builder.ToString();
>
>  //Bind
>  BindForm(null);
> }
>
>
>
> --
> Gregory A. Beamer
> MVP; MCP: +I, SE, SD, DBA
>
> ***************************
> Think Outside the Box!
> ***************************
>
>
> "ad" wrote:
>
>> I use a GridView to display data ,  the datasource of my GridView is from
>> a
>> ObjectDataSource.
>> How can I specify the sort column?
>>
>>
>>

AddThis Social Bookmark Button