Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 686
Dan Is Fiddling By Firelight Avatar asked Nov 30 '09 20:11

Dan Is Fiddling By Firelight


People also ask

What is the difference between DataRow and Datarowview?

The datarow is an object in a datatable. A DataView (usable with the name DefaultView) is a seperated table in the DataTable where the sort and rowselections are properties from.

What is DataRow C#?

A DataRow represent a row of data in data table. You add data to the data table using DataRow object. A DataRowCollection object represents a collection of data rows of a data table.

What is DataGridViewRow?

The DataGridViewRow class is used to access the individual cell elements, as well as to adjust the appearance and behavior of the row user interface (UI), such as height and cell style. Typically, you will want all rows or most rows in the control to share the same characteristics.


2 Answers

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.

like image 157
Neil Barnwell Avatar answered Sep 20 '22 14:09

Neil Barnwell


DataTable table = grdMyGrid.DataSource as DataTable; DataRow row = table.NewRow(); row = ((DataRowView)grdMyGrid.SelectedRows[0].DataBoundItem).Row; 
like image 38
Lev Z Avatar answered Sep 18 '22 14:09

Lev Z