Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGrid

Status
Not open for further replies.

Henryas

Programmer
Mar 28, 2005
16
0
0
US
Hi,

How do you get selected data from DataGrid after it has been sorted?

I have a DataGrid bound to a DataTable. When a user clicks on one of the rows, my program will get the row index (using DataGrid.CurrentRowIndex property) from the DataGrid, use the index to retrieve the appropriate row from the DataTable, and continue processing the retrieved data. This works fine, except after users click on the column header to sort the DataGrid. After the DataGrid is sorted by columns, apparently the index returned by the DataGrid does not matched with the index on the DataTable. Hence, my program is unable to get the proper data.

Any idea how to solve the problem?

Thanks.

Henry

 
If you set the DataSource to the DataTable, try using the DefaultView property:

Code:
//Initialized elsewhere
DataTable tbl;
DataGrid grd;

grd.DataSource = tbl;

//Sorting occurs

DataRowView rvw = tbl.DefaultView[grd.CurrentRowIndex];
DataRow row = rvw.Row;
//Treat the row as you normally would from the DataTable
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top