|
-
Aug 3rd, 2007, 04:46 PM
#1
Thread Starter
Member
[2005] Copy DatagridView Row to Another DataGridViewRow
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!
-
Aug 3rd, 2007, 10:15 PM
#2
Re: [2005] Copy DatagridView Row to Another DataGridViewRow
Are either or both of these grid's bound to a data source, like a DataTable or some other list?
-
Aug 3rd, 2007, 10:16 PM
#3
Thread Starter
Member
Re: [2005] Copy DatagridView Row to Another DataGridViewRow
No they're not. I forgot to mention that in the original post.
-
Aug 4th, 2007, 03:38 AM
#4
Re: [2005] Copy DatagridView Row to Another DataGridViewRow
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.
-
Aug 4th, 2007, 12:20 PM
#5
Thread Starter
Member
Re: [2005] Copy DatagridView Row to Another DataGridViewRow
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
-
Aug 4th, 2007, 09:05 PM
#6
Re: [2005] Copy DatagridView Row to Another DataGridViewRow
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.
-
Aug 4th, 2007, 09:11 PM
#7
Thread Starter
Member
Re: [2005] Copy DatagridView Row to Another DataGridViewRow
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.
-
Aug 4th, 2007, 10:12 PM
#8
Re: [2005] Copy DatagridView Row to Another DataGridViewRow
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.
-
Aug 7th, 2007, 08:02 AM
#9
Frenzied Member
Re: [2005] Copy DatagridView Row to Another DataGridViewRow
 Originally Posted by jmcilhinney
There are methods to copy a row within the same grid, which would be much more common.
JMC ... can you please explain to me how to copy a row, including the cell values, within the same DGV? Thanks...
-
Aug 7th, 2007, 08:20 AM
#10
Thread Starter
Member
Re: [2005] Copy DatagridView Row to Another DataGridViewRow
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
Last edited by paradoxperfect; Aug 7th, 2007 at 08:23 AM.
-
Dec 14th, 2011, 06:55 AM
#11
New Member
Re: [2005] Copy DatagridView Row to Another DataGridViewRow
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|