0

I tried this code to move selected rows from datagridview2 to datagridview1.But when I clicked to add button to move selected rows from datagridview2. And it also added to datagridview 2.But an error occurred through exception which is "Object reference not set to an instance of an object".How to solve this. My code as follows

 private void button1_Click(object sender, EventArgs e)
    {

        try
        {
            this.dataGridView1.Rows.Clear();
            foreach (DataGridViewRow row in dataGridView2.Rows)
            {
                int n = dataGridView1.Rows.Add();
                bool checkedCell = (bool)dataGridView2.Rows[n].Cells[0].Value;
                if (checkedCell == true)
                {
                    dataGridView1.Rows[n].Cells[0].Value = row.Cells[1].Value;
                    dataGridView1.Rows[n].Cells[1].Value = row.Cells[2].Value;
                    dataGridView1.Rows[n].Cells[3].Value = row.Cells[3].Value;
                }

            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(" Error " + ex.Message);
        }
    }

I need to do this.But data should load by clicking load button enter image description here

Kith
  • 117
  • 14
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – SᴇM Jun 07 '18 at 05:43
  • 1
    Possible duplicate of [Copying rows from one DataGridView to another](https://stackoverflow.com/questions/6891267/copying-rows-from-one-datagridview-to-another) – Vaelen Jun 07 '18 at 05:50
  • is it possible for you to provide more detail on the exception? which line is it thrown? etc. – AntiqTech Jun 07 '18 at 06:01
  • @AntiqTech its run foreachloop onece and throw the exception in "bool checkedCell = (bool)dataGridView2.Rows[n].Cells[0].Value;" – Kith Jun 07 '18 at 06:09
  • Possible duplicate of [How to transfer selected rows from one datagridview to another in c#?](https://stackoverflow.com/questions/50724698/how-to-transfer-selected-rows-from-one-datagridview-to-another-in-c) – Lucifer Jun 07 '18 at 06:27
  • 2
    _bool checkedCell = (bool)dataGridView2.Rows[n].Cells[0].Value;_ Shouldn't that be `bool checkedCell = (bool)row.Cells[0].Value;` ? Also: The code you show is not moving but copying. – TaW Jun 07 '18 at 07:01
  • @Adam it seems that you get exception because dataGridView2.Rows[n] is null. From the picture you added, it's clear that datagridviews have different numbers of rows. You are adding a new row, let's say 10th; other datagridview may not have a 10th row and even it has, that would be the incorrect row to check. As TaW pointed out in the previous comment, changing it to row.Cells[0].Value should fix the problem. – AntiqTech Jun 07 '18 at 09:25

1 Answers1

0

when you add row in datagridview2 you must set value of boolean column as same as :

  dataGridView2.Rows.Add("1", "2", "3", false);

other solution for do that:

      this.dataGridView1.Rows.Clear();
        foreach (DataGridViewRow row in dataGridView2.SelectedRows.Cast<DataGridViewRow>().ToList())
        {
            this.dataGridView1.Rows.Add(row.Cells[0].Value, row.Cells[1].Value, row.Cells[2].Value);
        }