How do I loop through a Datagridview control?? RRS feed

  • Question


    I have a datagridview control in a Windows form. I need to add the row(s) of the records to an Access database. I have a function call to add a row of record to the Access tableand it is working fine. How to loop through all the rows in the datagridview control and save the data one by one to the database? Also how do I filter out those row(s) for saving if certain conditions failed as in my case the value on one of the datagridviewtextbox can not be Null. Thanks.
    Friday, July 27, 2007 4:41 PM


  • Hi


    To loop through a DataGridView, try something like this:

    Code Snippet

                foreach (DataGridViewRow dgr in this.dataGridView1.Rows)


                    if (dgr.Cells[0].Value != null)


                        if (dgr.Cells[0].Value.ToString() != "")


    // your save method here





    But why not use data binding, it will make data updating much easier.


    Hope this helps.


    Monday, July 30, 2007 5:25 AM