What should I do? Firstly I need to create columns first, populate the DataGridViewColumns from the original to the new one. This is very straight forward, however when it comes to copy the rows. I came across a problem. The DataGridViewRow clone method does not work as expected. The Clone method copies the row and its property values, but does NOT copy the cell values that the row contains. So we have to write some code to copy the cell values. Please find the working example below and feel free to let me know should you have any queries.
private DataGridView CopyDataGridView(DataGridView dgv_org)
{
DataGridView dgv_copy = new DataGridView();
try
{
if (dgv_copy.Columns.Count == 0)
{
foreach (DataGridViewColumn dgvc in dgv_org.Columns)
{
dgv_copy.Columns.Add(dgvc.Clone() as DataGridViewColumn);
}
}
DataGridViewRow row = new DataGridViewRow();
for (int i = 0; i < dgv_org.Rows.Count; i++)
{
row = (DataGridViewRow)dgv_org.Rows[i].Clone();
int intColIndex = 0;
foreach (DataGridViewCell cell in dgv_org.Rows[i].Cells)
{
row.Cells[intColIndex].Value = cell.Value;
intColIndex++;
}
dgv_copy.Rows.Add(row);
}
dgv_copy.AllowUserToAddRows = false;
dgv_copy.Refresh();
}
catch (Exception ex)
{
cf.ShowExceptionErrorMsg("Copy DataGridViw", ex);
}
return dgv_copy;
}
great work. Thank you wery much!
ReplyDeleteReally Grate
Deletetu kab se yha par aa gaya re
DeleteThis comment has been removed by the author.
ReplyDeleteperfect !
ReplyDeletethank you so much!
how do you move the copied Row to the another DataGridView
ReplyDeleteCool work!!
ReplyDeleteGreat job ;)
ReplyDeletei want to copy only those rows which have been checked in one datagridview to another without including the check box column of the previous datagridview..please help..thanks in advance..
ReplyDeleteIn order to copy only the Selected Row
ReplyDeleteprivate DataGridView CopyDataGridView(DataGridView dgv_org)
{
DataGridView dgv_copy = new DataGridView();
try
{
if (dgv_copy.Columns.Count == 0)
{
foreach (DataGridViewColumn dgvc in dgv_org.Columns)
{
dgv_copy.Columns.Add(dgvc.Clone() as DataGridViewColumn);
}
}
DataGridViewRow row = new DataGridViewRow();
for (int i = 0; i < dgv_org.SelectedRows.Count; i++)
{
row = (DataGridViewRow)dgv_org.SelectedRows[i].Clone();
int intColIndex = 0;
foreach (DataGridViewCell cell in dgv_org.SelectedRows[i].Cells)
{
row.Cells[intColIndex].Value = cell.Value;
intColIndex++;
}
dgv_copy.Rows.Add(row);
}
dgv_copy.AllowUserToAddRows = false;
dgv_copy.Refresh();
}
catch (Exception ex)
{
System.Console.WriteLine(ex.ToString());
}
return dgv_copy;
}
}
}
yes but may i ask where do you copy-paste this code,in the form with original datagrid or the other one? Thx Bane
ReplyDeletesame question
DeleteHi,
ReplyDeleteI'm new at c#, but not at OOP.
I've tried to implement your code, but I'm not able to see the changes on the copied datagrid. The line I use to copy is like this:
this.dataGridView2=CopyDataGridView(this.dataGridView1);
I've tried to call .refresh, .update methods and so on, but the copied datagrid (created in dessign time) continue empty, althoug if I ask on debug immediate window dataGridView2.rowsCount, it returns 9.
I hope you can help me on this, because I'm actually stucked.
I wanted to reorder the dg1, but as I've been unable to do it because it's data-bounded, I thought it was possible to do it on a copy...
Thanks in advance!
Hello, Have you solved the problem? I have same problem like what you posted. I am interested in the final solution as well.
Deletesame problem, please im interested too in the final solution
DeleteThis comment has been removed by the author.
ReplyDeletehow binding data. Please me
ReplyDeletethis.dataGridView2=CopyDataGridView(this.dataGridView1);
check these examples.....C# Datagridview tutorial
ReplyDeleteLing
copying data from dtglist(datagridview1) to another dtgitem(datagridview2)...dtgitem is located in other form...vs2010
ReplyDeletehi, i don't know how to binding datagridview from a function , but i change the code to procedure (private DataGridView CopyDataGridView(DataGridView dgv_org) to private void CopyDataGridView(DataGridView dgv_org)), and replace dgv_copy with my second dgv, and call that procedure in rowsadded event from the first dgv, and its work
ReplyDeletecould you share your code as example?
DeleteThis comment has been removed by the author.
ReplyDelete'Hope This helps DGV1 is the datagridview
ReplyDelete'To copy Row
Private Sub CopyButton_Click(sender As System.Object, e As System.EventArgs) Handles CopyButton.Click
CopyRowIndex = DGV1.CurrentRow.Index
End Sub
'To Paste Row
Private Sub PasteButton_Click(sender As System.Object, e As System.EventArgs) Handles PasteButton.Click
PasteRowIndex = DGV1.CurrentRow.Index
For index As Int32 = 0 To DGV1.ColumnCount - 1
DGV1.Rows(CInt(PasteRowIndex)).Cells(index).Value = DGV1.Rows(CInt(CopyRowIndex)).Cells(index).Value
Next
End Sub
'To Duplicate Rows
Private Sub DuplicateButton_Click(sender As System.Object, e As System.EventArgs) Handles DuplicateButton.Click
CopyRowIndex = DGV1.CurrentRow.Index
DGV1.Rows.Add()
DuplicateRowIndex = DGV1.Rows.Count - 1
For index As Int32 = 0 To DGV1.ColumnCount - 1
DGV1.Rows(CInt(DuplicateRowIndex)).Cells(index).Value = DGV1.Rows(CInt(CopyRowIndex)).Cells(index).Value
Next
End Sub
Guys I have lot of procedures for copy, duplicate, delete etc. First I need to know if you want to work with 2 data grid views or want to do these functions in the same datagrid view. I can send you the code for the same.
ReplyDeleteHi Hanish
ReplyDeleteC# How to Copy a Selected Row From one GridView to another GridView With Mouse Click ?