Hi All,
I'm new to the forum. I was wondering if anyone could give me some suggestions for copying a datagridview's row to another datagridview. This would include the row's cell's contents also. Thanks in advance! :)
Printable View
Hi All,
I'm new to the forum. I was wondering if anyone could give me some suggestions for copying a datagridview's row to another datagridview. This would include the row's cell's contents also. Thanks in advance! :)
Are either or both of these grid's bound to a data source, like a DataTable or some other list?
No they're not. I forgot to mention that in the original post.
In that case you may be able to just remove a row from one grid and add it to the other. If the columns don't match then that would probably not be possible, in which case you just add a row to the second grid, copy the cell values from the first grid to the second, then remove the row from the first grid.
The only problem with that (and seems to be the only problem I'm running into) is that I just want to copy the row, not remove it from one. Thanks for the help :)
So, logically, you should add a new row to the second grid, then copy the values form the first to the second, then stop without removing the row from the first grid. If the columns in the two grids match then you can use the Clone method of the row to create the new row and then add it to the second grid. Clone does not copy the cell values so you have to do that yourself.
Thanks for the input. :) That is he way the I have been seeing it done. I do find it odd though that Microsoft wouldn't include such a function. I was just hoping there would be an easier way the doing a loop to collect each cell value. Thanks for the help though, it's much appreciated. :)
I'm afarid you're expecting a bit much. If Microsoft wrote such a method it would simply have to use a loop internally anyway, the same way you have to now. The fact that copying a row from one grid to another with exactly the same schema would be quite a rare thing is reason enough for them not to bother and to simply leave it up to the individual. There are methods to copy a row within the same grid, which would be much more common. Cloning a row and copying its cell values in a loop is a few lines of code. It's not much of an ordeal.
Quote:
Originally Posted by jmcilhinney
JMC ... can you please explain to me how to copy a row, including the cell values, within the same DGV? Thanks...
You can do something like this...
Code:DataGridView1.Rows.Add()
For i As Integer = 0 To DataGridView1.Rows(1).Cells.Count - 1
DataGridView1.Rows(1).Cells(i).Value = DataGridView1.Rows(0).Cells(i).Value
Next
Add ContextMenuStrip
Add Menu Name - Eg "Copy" or "Duplicate"
Select the same to your Context Menu Property of DB Grid
Add the following code in the click event
gridview.Rows.Insert(gridview.SelectedRows.Item(0).Index, gridview.Rows(gridview.SelectedRows.Item(0).Index).Cells(0).Value, gridview.Rows(gridview.SelectedRows.Item(0).Index).Cells(1).Value)'If you want more cells to copy from the Row repeat it after the coma
Suneer