C# .NET - How to copy dataGridView data to another?

Asked By Gokhan Metepe on 02-Oct-12 03:21 AM
I know that there are lots of articles about this matter. But non of them wprked for me.
I have 2 datagridviews and using 2 different binding sources. I want to copy data from first one to second. I have tried lots of ways. Some of them do nothing and some of them just copied Column types and Header texts, but did not copied data.
How can i copy them?

*I can copy everything, except data with these codes;

for (int j = 0; j < this.dgv1.Columns.Count; j++)
        this.dgv2.Columns.Add(this.dgv1.Columns[j].Clone() as DataGridViewColumn);
Rohan Dave replied to Gokhan Metepe on 02-Oct-12 04:27 AM
do something like below... I assume you have one Copy button and clicking on that button you need to copy data from one datagridview to another. So based on thsi your code looks like below..

DataTable objCopyDt = new DataTable();
objCopyDt = datagridview1.DataSource as DataTable;

datagrdiview2.DataSource = objCopyDt;

try by this...

Gokhan Metepe replied to Rohan Dave on 02-Oct-12 04:52 AM
I have tried this but when i changed ony cell in dgv2 the same cell changes on dgv1.
White Ashlin replied to Gokhan Metepe on 02-Oct-12 10:55 AM
You can try this free data export component: