DataGrid sorting causes rows to be out of sync with bound DataTable
-
I have bound a DataTable to a DataGrid: _ds = new System.Data.DataSet("ResultsDataSet"); _resultsTable = new System.Data.DataTable( "ResultsTable" ); _ds.Tables.Add( _resultsTable ); dataGridResults.SetDataBinding(_ds,"ResultsTable"); When the user clicks on a row, I want to get information about that row: System.Data.DataRow currRow = _resultsTable.Rows[dataGridResults.CurrentRowIndex]; This works fine until I sort the DataGrid by clicking on the column headers. Now the dataGridResults.CurrentRowIndex is out of sync with the _resultsTable. I either need to have the DataTable sort with the DataGrid, or be able to get information directly from the DataGrid. Am I doing something wrong, or do I need to add something?
-
I have bound a DataTable to a DataGrid: _ds = new System.Data.DataSet("ResultsDataSet"); _resultsTable = new System.Data.DataTable( "ResultsTable" ); _ds.Tables.Add( _resultsTable ); dataGridResults.SetDataBinding(_ds,"ResultsTable"); When the user clicks on a row, I want to get information about that row: System.Data.DataRow currRow = _resultsTable.Rows[dataGridResults.CurrentRowIndex]; This works fine until I sort the DataGrid by clicking on the column headers. Now the dataGridResults.CurrentRowIndex is out of sync with the _resultsTable. I either need to have the DataTable sort with the DataGrid, or be able to get information directly from the DataGrid. Am I doing something wrong, or do I need to add something?
Hi, try this:
System.Data.DataRow currRow = _resultsTable.DefaultView[dataGridResults.CurrentRowIndex].Row;
Robert
-
Hi, try this:
System.Data.DataRow currRow = _resultsTable.DefaultView[dataGridResults.CurrentRowIndex].Row;
Robert
That helped. I had to tinker with it a bit more, but it is working now. Thanks