我正在使用一个数据绑定的 Windows Forms DataGridView
。如果用户选择了一行,那么如何从 DataGridView
转到它的源 DataTable
的 DataRow
?
我正在使用一个数据绑定的 Windows Forms DataGridView
。如果用户选择了一行,那么如何从 DataGridView
转到它的源 DataTable
的 DataRow
?
DataRow row = ((DataRowView)DataGridViewRow.DataBoundItem).Row
假设您已绑定了普通的 DataTable
。
MyTypedDataRow row = (MyTypedDataRow)((DataRowView)DataGridViewRow.DataBoundItem).Row
假设你已经绑定了一个类型化的datatable。
更多信息请参见MSDN上的文章。
DataTable table = grdMyGrid.DataSource as DataTable;
DataRow row = table.NewRow();
row = ((DataRowView)grdMyGrid.SelectedRows[0].DataBoundItem).Row;
DataGridViewRow
中有一个名为DataBoundItem
的属性,类型为对象。DataRowView
(你可以检查一下确保它是这个类型)。 var row = (DataRowView) e.Row.DataItem;