locked
iterate through gridview row by row RRS feed

  • Question

  • I need to iterate through a gridview row by row. So for example, say I have a gridview that looks like the following:

    I now need to assign each row value to a variable when I iterate through the gridview. So the first set of values would be:

    Weight: 120     Height: 185     Age:27

    these values would then be inserted into a database. The inserting into the db is fine, its the iterating through the gridview which is the problem.

    Tuesday, August 14, 2012 1:54 PM

Answers

  • If you are not going to databind the grid such that it will handle the database update then:

    int Weight = new int(); int Height = new int(); int Age = new int(); foreach (DataGridViewRow dgRow in dataGridView1.Rows) { Weight = int.Parse(dgRow.Cells[0].Value.ToString()); Height = int.Parse(dgRow.Cells[1].Value.ToString()); Age = int.Parse(dgRow.Cells[2].Value.ToString());

    //insert values to database }



    Bob - www.crowcoder.com

    • Proposed as answer by milan.matejka Tuesday, August 14, 2012 2:30 PM
    • Marked as answer by Jason Dot Wang Wednesday, August 22, 2012 7:18 AM
    Tuesday, August 14, 2012 2:17 PM
  • Hi C-Sharp Mamba;

    Because a data grid view may have a row that has not yet been assigned a values you should check for it otherwise it will throw an exception.

    int weight = 0;
    int height = 0;
    int age = 0;
    
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        // Check the first cell to make use they have values before using it
        if (row.Cells[0].Value != null)
        {
            weight = (int) row.Cells[0].Value;
            height = (int) row.Cells[1].Value;
            age = (int) row.Cells[2].Value;
    
            // Do what you need here with the values.
        }
    }

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by Norkk Tuesday, August 14, 2012 3:56 PM
    • Marked as answer by Jason Dot Wang Wednesday, August 22, 2012 7:18 AM
    Tuesday, August 14, 2012 3:17 PM

All replies

  • If you are not going to databind the grid such that it will handle the database update then:

    int Weight = new int(); int Height = new int(); int Age = new int(); foreach (DataGridViewRow dgRow in dataGridView1.Rows) { Weight = int.Parse(dgRow.Cells[0].Value.ToString()); Height = int.Parse(dgRow.Cells[1].Value.ToString()); Age = int.Parse(dgRow.Cells[2].Value.ToString());

    //insert values to database }



    Bob - www.crowcoder.com

    • Proposed as answer by milan.matejka Tuesday, August 14, 2012 2:30 PM
    • Marked as answer by Jason Dot Wang Wednesday, August 22, 2012 7:18 AM
    Tuesday, August 14, 2012 2:17 PM
  • Hi C-Sharp Mamba;

    Because a data grid view may have a row that has not yet been assigned a values you should check for it otherwise it will throw an exception.

    int weight = 0;
    int height = 0;
    int age = 0;
    
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        // Check the first cell to make use they have values before using it
        if (row.Cells[0].Value != null)
        {
            weight = (int) row.Cells[0].Value;
            height = (int) row.Cells[1].Value;
            age = (int) row.Cells[2].Value;
    
            // Do what you need here with the values.
        }
    }

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by Norkk Tuesday, August 14, 2012 3:56 PM
    • Marked as answer by Jason Dot Wang Wednesday, August 22, 2012 7:18 AM
    Tuesday, August 14, 2012 3:17 PM
  • Hi C-Sharp Mamba,

      If you really want to know the way to implement iterating through DataGridView control row by row,please feel free to mark those replies as answer that gives you the satisfaction of helping.

      Sincerely,

      Jason Wang


    Jason Wang [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, August 16, 2012 4:27 AM