我正在使用数据绑定Windows窗体DataGridView
。如何从DataGridView
中的用户选择行转到DataRow
的DataTable
作为其来源?
DataRow row = ((DataRowView)DataGridViewRow.DataBoundItem).Row
假设你绑定了一个普通的DataTable
。
MyTypedDataRow row = (MyTypedDataRow)((DataRowView)DataGridViewRow.DataBoundItem).Row
假设你绑定了一个类型化的数据表。
有关更多信息,请参阅article on MSDN。
DataTable table = grdMyGrid.DataSource as DataTable;
DataRow row = table.NewRow();
row = ((DataRowView)grdMyGrid.SelectedRows[0].DataBoundItem).Row;
在一个DataGridViewRow
是一个名为DataBoundItem
类型对象的属性。
这将包含DataRowView
(为了确定你可以检查这个)
在Visual Studio 2017 .NET 4.5中,我取得了成功
var row = (DataRowView) e.Row.DataItem;