When I use a DataTable as a DataGridView DataSource , I often have to find which rows the user has selected and read certain values ββfrom the DataTable (and not the DataGridView)
I wanted to know if there is any reliable way to determine if a DataRow a DataGridViewRow is connected, primarily when the user has sorted the DataGridView in a different column from the default order.
I am currently using the following code:
Dim sorting = "" If DataGrid.SortOrder <> SortOrder.None Then 'get the actual row if the results are sorted' sorting = "[" & DataGrid.SortedColumn.Name & "]" & If(DataGrid.SortOrder = SortOrder.Ascending, "", " DESC") End If 'return the selected row after sorting' Dim selectedRowIndices = (From r In DataGrid.SelectedRows Select DirectCast(r, DataGridViewRow).Index).ToArray SelectedDataRow = (DataTable.Select("", sorting).Where( Function(r As DataRow, i As Integer) i = selectedRowIndex) ).FirstOrDefault
Thanks.
source share