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

Dan Is Fiddling By Firelight picture Dan Is Fiddling By Firelight · Nov 30, 2009 · Viewed 54.1k times · Source

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?

Answer

Neil Barnwell picture Neil Barnwell · Nov 30, 2009
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.