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