【问题标题】:How to determine which DataRow is bound to a DataGridViewRow如何确定哪个 DataRow 绑定到 DataGridViewRow
【发布时间】:2010-10-25 11:20:51
【问题描述】:

当我将DataTable 用作DataGridViewDataSource 时,我经常需要查找用户选择了哪些行并从DataTable(不是DataGridView)中读取特定值

我想知道是否有任何可靠的方法来确定 DataRowDataGridViewRow 绑定到哪个,尤其是当用户在非默认顺序的列上对 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


    【解决方案1】:

    DataGridViewRow.DataBoundItem 将为您提供数据绑定数据表(数据视图)中当前项目的数据视图行。

    所以:

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

    将为您提供 datagridviewrow 中的 DataRow。

    【讨论】:

    • 我马上试试看。
    猜你喜欢
    • 2011-11-04
    • 2011-12-10
    • 2011-01-28
    • 2011-02-08
    • 2015-03-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多