none
How To Loop through Datagridview

    Question

  • Hello Everyone, What I want is a loop that loops the entire datagridview  store the values into a data table. And then fill a Database with the above mentioned Datatable.
    Thursday, September 02, 2010 4:41 AM

Answers

  • Depending as well on the type of application you are developping. Nevertheless, at a first glance I would say that you can simply loop as following:

    foreach (DataRow row in ((DataTable)dataGrid1.DataSource).Rows)
    {
        MessageBox.Show(row["myColumn"].ToString());
    }

    or, depending of the level of access (to the data themselves or to the data in the control)

    foreach( DataGridViewRow row in dataGrid1.Rows)
    {
        MessageBox.Show(row["myColumn"].ToString());
    }

    Or if your doing some ASP.Net development, perhaps will you be interested in some script : http://www.thescarms.com/dotnet/WebDataGrid.aspx.

     

    Now, concerning populating a DataTable... Here I think you should implement some databinding for read/write the data and link them to the control. If you use 2-ways databinding then you will code the update operation so that what will be in the control will be stored in the DB. I think you should search for some tutorials because the subject is not small. To give a first idea I recommend for example that you read this article : http://www.switchonthecode.com/tutorials/csharp-tutorial-binding-a-datagridview-to-a-database.

    Thursday, September 02, 2010 5:10 AM
  • Hi,

    Depending on how you fill the DataTable to begin with you should be able to use a SqlDataAdapter to add, update, delete values in the DataBase depending on that changed in the DataTable.

    This article is for vb.net but it should be easy to guess what the C# counterparts should be.

    [How To Update a SQL Server Database by Using the SqlDataAdapter Object in Visual Basic .NET]

    http://support.microsoft.com/kb/308055/


    ---
    Happy Coding!
    Morten Wennevik [C# MVP]
    Thursday, September 02, 2010 5:24 AM

All replies

  • Depending as well on the type of application you are developping. Nevertheless, at a first glance I would say that you can simply loop as following:

    foreach (DataRow row in ((DataTable)dataGrid1.DataSource).Rows)
    {
        MessageBox.Show(row["myColumn"].ToString());
    }

    or, depending of the level of access (to the data themselves or to the data in the control)

    foreach( DataGridViewRow row in dataGrid1.Rows)
    {
        MessageBox.Show(row["myColumn"].ToString());
    }

    Or if your doing some ASP.Net development, perhaps will you be interested in some script : http://www.thescarms.com/dotnet/WebDataGrid.aspx.

     

    Now, concerning populating a DataTable... Here I think you should implement some databinding for read/write the data and link them to the control. If you use 2-ways databinding then you will code the update operation so that what will be in the control will be stored in the DB. I think you should search for some tutorials because the subject is not small. To give a first idea I recommend for example that you read this article : http://www.switchonthecode.com/tutorials/csharp-tutorial-binding-a-datagridview-to-a-database.

    Thursday, September 02, 2010 5:10 AM
  • thanks but I have a datagridview .which have new values added without updating it to the database how do i loop through each record and update it to the database. ?

     

    Thursday, September 02, 2010 5:13 AM
  • i Have this code for adding values to datagridview that is not reflected in database.

    Later on a button click I want to update values to the database which will be final.

    Code:

    private void button1_Click(object sender, EventArgs e)

            {

                addnewrow();

    }

            private void addnewrow()

            {

     

                DataRow row = dt.NewRow();

                row["tab id"] = this.textBox1.Text.ToString();

                row["tab name"] = this.textBox2.Text.ToString();

                dt.Rows.Add(row);

            }

    Thursday, September 02, 2010 5:16 AM
  • Hi,

    Depending on how you fill the DataTable to begin with you should be able to use a SqlDataAdapter to add, update, delete values in the DataBase depending on that changed in the DataTable.

    This article is for vb.net but it should be easy to guess what the C# counterparts should be.

    [How To Update a SQL Server Database by Using the SqlDataAdapter Object in Visual Basic .NET]

    http://support.microsoft.com/kb/308055/


    ---
    Happy Coding!
    Morten Wennevik [C# MVP]
    Thursday, September 02, 2010 5:24 AM