How do I get a DataRow from a row in a DataGridView

61,288

Solution 1

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.

Solution 2

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

Solution 3

In a DataGridViewRow is a property called DataBoundItem of type object.

This will contain a DataRowView (for certainty you can check this)

Share:
61,288
Dan Is Fiddling By Firelight
Author by

Dan Is Fiddling By Firelight

Updated on January 25, 2020

Comments

  • Dan Is Fiddling By Firelight
    Dan Is Fiddling By Firelight over 4 years

    I'm using a databound Windows Forms DataGridView. how do I go from a user selected row in the DataGridView to the DataRow of the DataTable that is its source?