【问题标题】:How do I get a DataRow from a row in a DataGridView如何从 DataGridView 中的一行获取 DataRow
【发布时间】:2010-12-21 19:03:29
【问题描述】:

我正在使用数据绑定的 Windows 窗体 DataGridView。如何从DataGridView 中用户选择的行转到作为其来源的DataTableDataRow

【问题讨论】:

    标签: c# winforms datagridview datatable


    【解决方案1】:
    DataRow row = ((DataRowView)DataGridViewRow.DataBoundItem).Row
    

    假设你绑定了一个普通的DataTable

    MyTypedDataRow row = (MyTypedDataRow)((DataRowView)DataGridViewRow.DataBoundItem).Row
    

    假设您已经绑定了一个类型化的数据表。

    请参阅article on MSDN 了解更多信息。

    【讨论】:

    【解决方案2】:
    DataTable table = grdMyGrid.DataSource as DataTable;
    DataRow row = table.NewRow();
    row = ((DataRowView)grdMyGrid.SelectedRows[0].DataBoundItem).Row;
    

    【讨论】:

      【解决方案3】:

      DataGridViewRow 中有一个名为DataBoundItem 的对象类型的属性。

      这将包含一个DataRowView(您可以检查一下)

      【讨论】:

        【解决方案4】:

        在 Visual Studio 2017 .NET 4.5 中,我取得了成功

         var row = (DataRowView) e.Row.DataItem;
        

        【讨论】:

        • 这是 ASP.Net,不是 WinForms。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-09-01
        • 1970-01-01
        • 2013-06-09
        • 1970-01-01
        • 2012-06-10
        • 1970-01-01
        • 2010-11-21
        相关资源
        最近更新 更多