How to specify the sort column of a GridView

  • Thread starter Thread starter ad
  • Start date Start date
A

ad

I use a GridView to display data , the datasource of my GridView is from a
ObjectDataSource.
How can I specify the sort column?
 
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!
***************************
 
Thanks,
But I do'nt how to add event handler for sort (Setp2)
Could you tell me more detail?


Cowboy (Gregory A. Beamer) - MVP said:
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 said:
I use a GridView to display data , the datasource of my GridView is from
a
ObjectDataSource.
How can I specify the sort column?
 
Back
Top