How do I get a DataRow from a row in a DataGridView
I'm using a databound Windows Forms DataGridView
. how do I go from a user selected row in the DataGridView
to the开发者_如何学Python DataRow
of the DataTable
that is its source?
DataRow row = ((DataRowView)DataGridViewRow.DataBoundItem).Row
Assuming you've bound an ordinary DataTable
.
MyTypedDataRow row = (MyTypedDataRow)((DataRowView)DataGridViewRow.DataBoundItem).Row
Assuming you've bound a typed datatable.
See the article on MSDN for more information.
DataTable table = grdMyGrid.DataSource as DataTable;
DataRow row = table.NewRow();
row = ((DataRowView)grdMyGrid.SelectedRows[0].DataBoundItem).Row;
In a DataGridViewRow
is a property called DataBoundItem
of type object.
This will contain a DataRowView
(for certainty you can check this)
In Visual Studio 2017 .NET 4.5, I had success with
var row = (DataRowView) e.Row.DataItem;
精彩评论