如何从DataGridView中的行获取DataRow

问题描述 投票:30回答:4

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

c# winforms datagridview datatable
4个回答
40
投票
DataRow row = ((DataRowView)DataGridViewRow.DataBoundItem).Row

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

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

假设你绑定了一个类型化的数据表。

有关更多信息,请参阅article on MSDN


8
投票
DataTable table = grdMyGrid.DataSource as DataTable;
DataRow row = table.NewRow();
row = ((DataRowView)grdMyGrid.SelectedRows[0].DataBoundItem).Row;

2
投票

在一个DataGridViewRow是一个名为DataBoundItem类型对象的属性。

这将包含DataRowView(为了确定你可以检查这个)


0
投票

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

 var row = (DataRowView) e.Row.DataItem;
© www.soinside.com 2019 - 2024. All rights reserved.