locked
Saving DataGridView records to a DataTable RRS feed

  • Question

  • My loop is not working properly when I try to save data grid view records into a data table...




    Everything is working fine until I get at the 3rd row of the data grid view where there is nothing.  Here is my code:

    private void btnSave_Click(object sender, EventArgs e)
            {
                DialogResult myResult;
                myResult = MessageBox.Show("The records will be saved for processing.", "Delete Confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                if (myResult == DialogResult.OK)
                {
                    DataTable dt = new DataTable();
                    foreach (DataGridViewColumn oDataGridViewColumn in dgv.Columns)
                    {
                        dt.Columns.Add(oDataGridViewColumn.Name);
                    }

                    foreach (DataGridViewRow oDataGridViewRow in dgv.Rows)
                    {

                        for (int j = 0; j < (this.dgv.RowCount - 1); j++)
                        {
                            DataRow dr = dt.NewRow();
                          {

                                for (int i = 0; i < this.dgv.ColumnCount; i++)
                                {
                                    dr[i] = oDataGridViewRow.Cells[i].Value.ToString(); ;
                                }
                                util.CansimInsert(dr);
                            }
                        }
                    }

                    dgv.Refresh();
                }
                else
                {
                    return;
                }



    The following line gives me an error message because the value is null:  dr[i] = oDataGridViewRow.Cells[i].Value.ToString(); ;

    Can somebody tell me why??? 

    Thanks


    M. Chalut
    Monday, July 27, 2009 3:43 PM

Answers

  • It's working Tergiver.  Here is my code...  Thanks!!

    private void btnSave_Click(object sender, EventArgs e)
            {
                DialogResult myResult;
                myResult = MessageBox.Show("The records will be saved for processing.", "Delete Confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                if (myResult == DialogResult.OK)
                {
                    DataTable dt = new DataTable();
                    foreach (DataGridViewColumn oDataGridViewColumn in dgv.Columns)
                    {
                        dt.Columns.Add(oDataGridViewColumn.Name);
                    }

                    foreach (DataGridViewRow oDataGridViewRow in dgv.Rows)
                    {

                    if (oDataGridViewRow.Index != dgv.NewRowIndex)
                    {

                            DataRow dr = dt.NewRow();
                          {

                                for (int i = 0; i < this.dgv.ColumnCount; i++)
                                {
                                    dr[i] = oDataGridViewRow.Cells[i].Value.ToString(); ;
                                }
                                util.CansimInsert(dr);
                            }
                                     } 
                        }
                    }

                    dgv.Refresh();
                }
                else
                {
                    return;
                }


    M. Chalut
    • Marked as answer by Mylen33 Wednesday, July 29, 2009 11:03 AM
    Monday, July 27, 2009 10:37 PM

All replies

  • That "3rd row" is the "new row". It's always empty. To identify it you use: NewRowIndex.

    if (oDataGridViewRow.Index != dgv.NewRowIndex)
    {
    }

    • Marked as answer by Mylen33 Monday, July 27, 2009 10:37 PM
    • Unmarked as answer by Mylen33 Tuesday, July 28, 2009 5:44 PM
    Monday, July 27, 2009 4:03 PM
  • you can also get datable using.

    DataTable dt = (DataTable)dataGridView1.DataSource;

    not the solution but might helpful to u.
    Let's Program.. , Yam Sapkota , http://www.freevideotutorial.net
    • Proposed as answer by JohnGrove Monday, July 27, 2009 10:09 PM
    Monday, July 27, 2009 8:00 PM
  • It's working Tergiver.  Here is my code...  Thanks!!

    private void btnSave_Click(object sender, EventArgs e)
            {
                DialogResult myResult;
                myResult = MessageBox.Show("The records will be saved for processing.", "Delete Confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                if (myResult == DialogResult.OK)
                {
                    DataTable dt = new DataTable();
                    foreach (DataGridViewColumn oDataGridViewColumn in dgv.Columns)
                    {
                        dt.Columns.Add(oDataGridViewColumn.Name);
                    }

                    foreach (DataGridViewRow oDataGridViewRow in dgv.Rows)
                    {

                    if (oDataGridViewRow.Index != dgv.NewRowIndex)
                    {

                            DataRow dr = dt.NewRow();
                          {

                                for (int i = 0; i < this.dgv.ColumnCount; i++)
                                {
                                    dr[i] = oDataGridViewRow.Cells[i].Value.ToString(); ;
                                }
                                util.CansimInsert(dr);
                            }
                                     } 
                        }
                    }

                    dgv.Refresh();
                }
                else
                {
                    return;
                }


    M. Chalut
    • Marked as answer by Mylen33 Wednesday, July 29, 2009 11:03 AM
    Monday, July 27, 2009 10:37 PM
  • I would recommend extrapolating the DataTable from the DataSource as Yam has suggested, or at the very least getting it's structure.

    DataTable mainTable = (DataTable)dataGridView1.DataSource;
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    if (row.IsNewRow)
                    {
                        if (row.Cells["Selection"].Value != null)
                        {
                            mainTable.Rows.Add(row.Cells["Selection"].Value,
                                row.Cells[1].Value.ToString(),
                                row.Cells[2].Value.ToString(),
                                row.Cells[3].Value.ToString(),
                                row.Cells[4].Value.ToString(),
                                row.Cells[5].Value.ToString(),
                                row.Cells[6].Value.ToString());
                        }
                    }
                }
                dataGridView1.DataSource = mainTable;
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Edited by JohnGrove Monday, July 27, 2009 10:42 PM
    Monday, July 27, 2009 10:41 PM
  • I will try this tomorrow at work...  Thanks guys!!


    M. Chalut
    Monday, July 27, 2009 10:56 PM
  • Yea, it sounds like you're doing things backwords. Usually you would have a data table and you would bind the DGV to the table. It handles quite a lot of things all on its own.
    Tuesday, July 28, 2009 2:37 AM