如何确定哪个DataRow绑定到一个DataGridViewRow [英] How to determine which DataRow is bound to a DataGridViewRow
问题描述
当我使用 DataTable
作为 DataGridView
的 DataSource
,我经常需要找到用户选择的行,并从 DataTable (不是DataGridView)
When I use a DataTable
as a DataGridView
's DataSource
, I often have to find which row(s) a user has selected and read specific values from the DataTable (not the DataGridView)
我想知道是否有任何可靠的方式来确定哪个 DataRow
a DataGridViewRow
被绑定到especia当用户在除默认顺序之外的列中对DataGridView进行排序时,请执行以下操作。
I wanted to know if there is any reliable way of determining which DataRow
a DataGridViewRow
is bound to especially when the user has sorted the DataGridView on a column other than the default order.
目前,我使用这样的代码:
Currently, I use code of this sort:
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
谢谢。 p>
Thanks.
推荐答案
DataGridViewRow.DataBoundItem将为Databound dataTable(dataview)中的当前项提供一个dataViewRow。
DataGridViewRow.DataBoundItem will give you a dataViewRow for the current item in the Databound dataTable (dataview).
所以:
Dim drv as DataRowView = myDataGridViewRow.DataBoundItem
Dim dr as DataRow = drv.Row
将给你DataRow从您的datagridviewrow。
Will give you the DataRow from your datagridviewrow.
这篇关于如何确定哪个DataRow绑定到一个DataGridViewRow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!