DataGrid What column is selected on Sort Event

  • Thread starter Ben Helppi via .NET 247
  • Start date
B

Ben Helppi via .NET 247

I've been struggling to get functionality out of the DataGrid. The documents that I found weren't any help, so I was wondering if someone could tell me how to find out what Column was pressed on a sort event.

private void MyDataGrid_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
 
I

imsinc

There is no easy way to do this unfortuanately but it is possible. Try
this:

string[] ctrlParts = Request.Form["__EVENTTARGET"].Split(":");

if (ctrlParts[0] == YourDataGrid.ID)
{
int whichColum = int.Parse(ctrlParts[2].Substring(4)) + 1;
}

The only part you should know is that columns are indexed from 1, not 0.
 
E

Elton Wang

Hi Ben,

Normally e.SortExpression can be used to determine the
column will be sorted.


HTH


Elton Wang
(e-mail address removed)
-----Original Message-----
I've been struggling to get functionality out of the
DataGrid. The documents that I found weren't any help, so
I was wondering if someone could tell me how to find out
what Column was pressed on a sort event.
private void MyDataGrid_SortCommand(object source,
System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
 
B

Brock Allen

Normally e.SortExpression can be used to determine the column will be

And just in case you haven't done this, using e.SortExpression requires you
to put the SortExpression attribute on your columns in the <Columns> template
in your ASPX. For example:

<asp:DataGrid ...>
<Columns>
<asp:BoundColumn HeaderText="FirstName" ... SortExpression="au_fname"
/>
<asp:BoundColumn HeaderText="LastName" ... SortExpression="au_lname" />
</Columns>
</asp:DataGrid>

e.SortExpression is whatever you put as the SortExpression.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top