How to determine which DataRow is bound to a DataGridViewRow

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.

0
source share
1 answer

DataGridViewRow.DataBoundItem will provide you with a dataViewRow for the current item in a DataTable DataTable (dataview).

So:

 Dim drv as DataRowView = myDataGridViewRow.DataBoundItem Dim dr as DataRow = drv.Row 

Gives you a DataRow from your datagridviewrow.

+4
source

Source: https://habr.com/ru/post/1369065/


All Articles