【发布时间】:2010-10-25 11:20:51
【问题描述】:
当我将DataTable 用作DataGridView 的DataSource 时,我经常需要查找用户选择了哪些行并从DataTable(不是DataGridView)中读取特定值
我想知道是否有任何可靠的方法来确定 DataRow 和 DataGridViewRow 绑定到哪个,尤其是当用户在非默认顺序的列上对 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
谢谢。
【问题讨论】:
标签: c# .net vb.net data-binding datagridview