Results 1 to 9 of 9

Thread: Copy a record from one datagrid view to another

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Posts
    23

    Copy a record from one datagrid view to another

    Hello All

    I am writing a programme that copies the selected row in one datagridview to a new row in a second datagridview when a button is clicked.

    Can someone tell me the best way to go about this?

    This is my code thus far:

    Code:
    Imports System.Data.OleDb
    
    Public Class Form1
        Dim cs As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\VB\DBSelectAndAddTest\addtest.accdb")
        Dim daCat As New OleDbDataAdapter("SELECT Catalogues.* from Catalogues", cs)
        Dim daOrd As New OleDbDataAdapter("SELECT * from Order", cs)
        Dim ds As New DataSet
      
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            daCat.Fill(ds, "Catalogue")
            daOrd.Fill(ds, "Order")
    
            dgcats.DataSource = ds.Tables("Catalogue")
            dgorder.DataSource = ds.Tables("Order")
    
        End Sub

  2. #2
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Copy a record from one datagrid view to another

    i can see 2 bound datagridview controls in your code
    what you want to do here
    next is there are few good way of copying from one datagridview to another

    but you need to make it clear ,if i assume that you will select a row and the click a button and that row to be copy to another gridview ?
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Posts
    23

    Re: Copy a record from one datagrid view to another

    Quote Originally Posted by make me rain View Post
    i can see 2 bound datagridview controls in your code
    what you want to do here
    next is there are few good way of copying from one datagridview to another

    but you need to make it clear ,if i assume that you will select a row and the click a button and that row to be copy to another gridview ?
    Correct I have two bound datagridviews.

    The first is a catalogue of products and the second represents an order for which I want to add products from the catalogue to.

    I want to be able to highlight a row on the catalogue datagrid, then click a button that will copy the contents of the highlighted row to the order datagrid (both datagrids will have similar columns).

    Does this give you enough info?

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Copy a record from one datagrid view to another

    As you said both have the same structure then use DataTable.ImportRow Method

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Posts
    23

    Re: Copy a record from one datagrid view to another

    Will this only work if they have exactly that same data structure?

  6. #6
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Copy a record from one datagrid view to another

    Quote Originally Posted by Yippiekaiaii View Post
    Will this only work if they have exactly that same data structure?
    Yes, both need to have the same data structure otherwise a column that is not defined will raise an exception as per MSDN documentation.

  7. #7
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Copy a record from one datagrid view to another

    If you have VS2010 then try the attached project which shows how to clone one DataGridView to another DataGridView where both have the same columns and also shows the same but the second time where the source and both do have the same primary key and one other common column but the target has a column that does not exists in the source (might sound confusing be it will not after you try it).

    Okay, load the project, compile and run. Double click on the source DataGridView (the top one) and the middle DataGridView is cloned followed by the top DataGridView doing the bottom DataGridView but not matching up columns.

    There is protection built in so that you cannot duplicate a row in one of the targets.

    There is a bit of code to do the above but if you follow what I did all you really see is

    Code:
    Private Sub DataGridView1_MouseDoubleClick() Handles TopGrid.MouseDoubleClick
        '
        ' Match columns
        '
        bsCustomers.CloneCurrentRow(bsCustomersClone, "Identifier", True, True)
    End Sub
    Or

    Code:
    Private Sub DataGridView1_MouseDoubleClick() Handles TopGrid.MouseDoubleClick
        '
        ' Do not match columns
        '
        bsCustomers.CloneCurrentRow(bsThird, "Identifier", True, False)
    End Sub
    In closing if something does not make sense yet works you might consider researching this and if no answer is found ask me but research first so that you learn.
    Attached Files Attached Files

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Posts
    23

    Re: Copy a record from one datagrid view to another

    That programme does almost exactly what i am after.

    It uses some code I havent come across yet though.

    You have used this code at the start:
    Code:
    WithEvents bsThird As New BindingSource
    What does "WithEvents" mean?

    Also on this part of the code:
    Code:
    bsCustomers.CloneCurrentRow(bsThird, "Identifier", True, False)
    What does the, True, False part of the statement signify?

    Sorry I am asking a lot of questions but I am new to this

  9. #9
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Copy a record from one datagrid view to another

    Quote Originally Posted by Yippiekaiaii View Post
    You have used this code at the start:
    Code:
    WithEvents bsThird As New BindingSource
    What does "WithEvents" mean?
    WithEvents, Google search

    Quote Originally Posted by Yippiekaiaii View Post

    What does the, True, False part of the statement signify?
    They are documented in the code.

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