Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C#3.0 C#4.0
Hi All,
Need some feedback, I am trying to copy a column value from datagridview1 to dgv2. If I pressed the button for the first time, it will create a column with the column name from dgv1 and copy the values to an empty dgv2, which is correct.
However, when I press the button again, it will create a new column, but will replace all values in the first column. How do I set the column index so that each time I press the button, the values are copied to the correct column?
Thanks in advance.


private void button4_Click(object sender, EventArgs e)
          List<int> selectedColumns = new List<int>();
          foreach (DataGridViewColumn col in  dataGridView1.Columns)
              if (col.Index == columIndexFromMouseDown)
              int rowIndex = 0;
              foreach (DataGridViewRow row in dataGridView1.Rows)
                  for (int i = 0; i < selectedColumns.Count ; i++)
                      if (dataGridView2.Columns.Count ==1)
                          dataGridView2.Rows[rowIndex].Cells[i].Value = row.Cells[selectedColumns[i]].Value;
                      if (dataGridView2.Columns.Count > 1)
Posted 3-Oct-12 23:33pm
vijay bisht at 4-Oct-12 6:28am
on ur button 4 click please remove the added columns form datagridview2
and then do ur work ....reply me if it works

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 686
1 OriginalGriff 345
2 CPallini 240
3 PIEBALDconsult 150
4 Magic Wonder 131
0 OriginalGriff 5,795
1 Sergey Alexandrovich Kryukov 5,158
2 CPallini 4,740
3 George Jonsson 3,167
4 Gihan Liyanage 2,450

Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 4 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100