How to get a DataRow from a row in a DataGridView

I am using Windows Forms DataGridView data binding. how can I go from a user-selected row in a DataGridView to a DataRow DataTable , which is its source?

+29
source share
4 answers
 DataRow row = ((DataRowView)DataGridViewRow.DataBoundItem).Row 

Assuming you bound a regular DataTable .

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

Assuming you bound a typed data type.

See the article on MSDN for more information.

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

There is a property in the DataGridViewRow called the DataBoundItem of the type object.

This will contain a DataRowView (you can verify this for certain)

+2
source

In Visual Studio 2017.NET 4.5, I had success

  var row = (DataRowView) e.Row.DataItem; 
0
source

Source: https://habr.com/ru/post/1369061/


All Articles