DarioSantarelli.Blog(this);

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 😉

17 Responses to “Getting a DataRow from a DataGridViewRow”

  1. eason said

    thx. It help

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

  3. Alejandra said

    Thanks!!!

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

  5. Nicolas Raflaowski said

    thx its great 🙂
    !!!!!!!!!!!!

  6. not working for me either

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

  8. xancliman said

    GENIAL!! eres un un crack!

  9. savigue said

    Thx very much for help!!!

  10. Oh Thanks My Friend!

  11. taco said

    domo arigato!!

  12. 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.,

  13. Francisco Rossi said

    it helped a lot, thank you very much

  14. 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?

  15. Vikrant takkar said

    Thank you very much.

  16. 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”))

Leave a reply to Alejandra Cancel reply