C# 如何确定哪个DataRow绑定到DataGridViewRow

C# 如何确定哪个DataRow绑定到DataGridViewRow,c#,.net,vb.net,data-binding,datagridview,C#,.net,Vb.net,Data Binding,Datagridview,当我使用a作为a时,我通常必须找到用户选择的行,并从中读取特定值,而不是从DataGridView 我想知道是否有可靠的方法来确定绑定到哪个列,特别是当用户将DataGridView排序到默认顺序以外的列时 目前,我使用这类代码: Dim sorting = "" If DataGrid.SortOrder <> SortOrder.None Then 'get the actual row if the results are sorted' sorting =

当我使用a作为a时,我通常必须找到用户选择的行,并从中读取特定值,而不是从DataGridView

我想知道是否有可靠的方法来确定绑定到哪个列,特别是当用户将DataGridView排序到默认顺序以外的列时

目前,我使用这类代码:

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

谢谢。

DataGridViewRow.DataBoundItem将为数据绑定dataTable数据视图中的当前项提供一个dataViewRow

因此:


将为您提供datagridviewrow中的DataRow。

datagridviewrow.DataBoundItem将为您提供Databound dataTable dataview中当前项的dataViewRow

因此:


将从datagridviewrow中为您提供数据行。

我马上就要尝试。我马上就要尝试。
Dim drv as DataRowView = myDataGridViewRow.DataBoundItem

Dim dr as DataRow = drv.Row