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
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 ?
Re: Copy a record from one datagrid view to another
Originally Posted by make me rain
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).
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.