如何从DataGridView中的行获取DataRow

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

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

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

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

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

有关更多信息,请参阅MSDN上的文章 。

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

DataGridViewRow是一个名为DataBoundItem的属性,类型为object。

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