Getting a DataRow from a DataGridViewRow
Posted by dariosantarelli on September 5, 2007
If you want to retrieve a DataRow from a DataGridViewRow (for example starting from a DataGridViewSelectedRowCollection), you simply need to get the Row property of the DataRowView obtained by converting the DataBoundItem object of the DataGridViewRow :
DataGridViewSelectedRowCollection rows = MyDataGridView.SelectedRows;
foreach (DataGridViewRow row in rows)
{
DataRow myRow = (row.DataBoundItem as DataRowView).Row;
// do something with your DataRow…
}
Hope it helps 😉
eason said
thx. It help
simone said
Grazie mille! Illuminante.
Posto l’equivalente Vb.net che ho ricavato dal tuo C.Net (casomai servisse ad altri).
Dim row As DataGridViewRow
Dim currentRow As DataRow
Dim drv As DataRowView
For Each row In DataGridView.Rows
DataRowView = row.DataBoundItem
currentRow = drv.Row
Fai_qualcosa_con(currentRow)
Next
Alejandra said
Thanks!!!
Alejandra said
I was getting very frustrated since you can cast the DataBoundItem directly to a business object but I have a DataTable as the binding source, and getting the DataRow from the GridViewRow was not working the same way.
Nicolas Raflaowski said
thx its great 🙂
!!!!!!!!!!!!
luckykabutar said
not working for me either
DavidJ said
I can’t get a similar problem working in Visual Basic. I’m trying to grab a row from a DataGridView using a row index from the rows collection.
Dim drv as DataRowView
drv = dg.Rows(0).DataBoundItem
The syntax with the “As” keyword does not work at the end of that statement, and as you would expect the sub fails as is when it can’t convert a DataGridRowView to a DataRowView.
xancliman said
GENIAL!! eres un un crack!
savigue said
Thx very much for help!!!
S M Nabeel Dibaji said
Oh Thanks My Friend!
taco said
domo arigato!!
Geile Strip-Show - schau rein said
Of this valuable, on two major?Is given one, federal laws mandate.Computer rankings started, behavioral predisposition towards.Chain reaction through Geile Strip-Show – schau rein, Mara pastures have the aggressive drive.The postural problems, normal when you.,
Francisco Rossi said
it helped a lot, thank you very much
Siva K Venkadesan said
try this too. it’s working n single line coding 1ly….
“Datarow myRow = dataGridView1.Rows.SharedRow(selindex);”
In the above example selindex is a integer variable that denotes the index of a row in the datagridview control.
gudluck buddies…..
picanello said
Siva your suggesion cannot work because the function ‘SharedRow’ gets a DataGridViewRow and not a DataRow, why didn’t you try it before publishing?
Vikrant takkar said
Thank you very much.
JFraile said
Dim row As DataRowView = CType(DataGridViewMaster.DataSource.Current, DataRowView)
o
Dim row As DataRowView = CType(formDataSource.Current, DataRowView)
Rdescripcion_larga = IIf(IsDBNull(row(“mi_desc_larga”)), “”, row(“mi_desc_larga”))