Results 1 to 11 of 11

Thread: [2005] Copy DatagridView Row to Another DataGridViewRow

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    38

    [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!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    38

    Re: [2005] Copy DatagridView Row to Another DataGridViewRow

    No they're not. I forgot to mention that in the original post.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    38

    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

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    38

    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.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: [2005] Copy DatagridView Row to Another DataGridViewRow

    Quote 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...

  10. #10

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    38

    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.

  11. #11
    New Member
    Join Date
    Dec 2011
    Posts
    1

    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
  •  



Click Here to Expand Forum to Full Width