DataGrid AllowSort Issue

  • Thread starter Thread starter Prasanna Ekanayake
  • Start date Start date
P

Prasanna Ekanayake

Hi,



How can I get the DataRow that the user selected/clicked in an AllowSort
enabled DataGrid? Given below is what I'm currently trying to do to achieve
it.



I have a DataTable that is the DataSource to my DataGrid. The DataTable
contains all data from my SalesRep table which contains the following
columns.

UID - Identity Column

ID - UserID [Eg: BD00001]

FirstName

MiddleInitials

LastName

Address

Phone

Fax

Email



In the DataGrid all columns are displayed EXCLUDING the UID column. I have
enable AllowSort. Before sorting the DataGrid (by clicking the Column
header), I can get the UID of the selected/clicked row.



MessageBox.Show( ( (DataTable) cdg
cdgSalesRep.DataSource).Rows[cdgSalesRep.CurrentRowIndex][0].ToString() );



But soon as the DataGrid is sorted (by clicking the Column header), the
values return by the above statement is wrong.



If someone could point out what I'm doing wrong it would be a great help.
What I really want to do is to retrieve the DataRow that the user
selected/clicked.



Thanks in Advance!



Prasanna.
 
Hi,

The only working way I am aware of is looking up the row by the primary key.
That is, every time the user selects a row, you remember its primary key,
and when the sort order changes, you look up the remembered key value in the
DataView available through the CurrencyManager.List property and set the
CurrencyManager's Position property to the index of the found row.

The CurrencyManager instance can be obtained through

CurrencyManager cm = (CurrencyManager)this.BindingContext[grid.DataSource,
grid.DataMember];
 
Back
Top