Search code examples
c#winformsdatagridviewdatatable

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 DataRow of the DataTable that is its source?


Solution

  • 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.