Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have 4 columns in first datagridview which is dgvProducts:
item id
item name
price
category


and 4 columns also in my 2nd datagridview named dgvCart:
item id
item name
price
quantity


when a row in dgvProducts is clicked, there will be a dialogbox that prompts them if how many are they going to order in that specific product.. in that dialogbox there is a label, txtbox(txtQuantity) and a button(Add to Cart). the values in 1st, 2nd, 3rd cell of dgvProducts selected row is copied going to 1st, 2nd and 3rd cell of dgvCart. and the value for 4th cell of dgvCart will come from the txtQuantity.

i used this code to bind data from a DB Access to dgvProducts:
VB
Dim X As New OleDb.OleDbConnection("Provider = microsoft.jet.oledb.4.0; data source = SourceFile.mdb")
Dim Y As New OleDb.OleDbDataAdapter("Select * from tbkProducts", X)


im creating bakery ordering system.

dgvProducts is connected to a DB MS Access.
dgvCart is empty. (just have columns, created via designer property)

i tried the code below but it copies all the cell values in a selected row. and only the first row is being copied to the other datagridview. pls im new in using datagridview i really need help. what should i change in the code?
thank you in advance.

What I have tried:

VB
Private Sub dgvProducts_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgvProducts.CellMouseClick
        If dgvCart.Columns.Count = 0 Then
            For Each dgvc As DataGridViewColumn In dgvProducts.Columns
                dgvCart.Columns.Add(TryCast(dgvc.Clone(), DataGridViewColumn))
            Next
        End If

        Dim row As New DataGridViewRow()
        For i As Integer = 0 To dgvProducts.Rows.Count = 1
            row = DirectCast(dgvProducts.Rows(i).Clone(), DataGridViewRow)
            Dim intColIndex As Integer = 0
            For Each cell As DataGridViewCell In dgvProducts.Rows(i).Cells
                row.Cells(intColIndex).Value = cell.Value
                intColIndex += 1
            Next
            dgvCart.Rows.Add(row)
        Next

    End Sub
Posted
Updated 11-Mar-18 20:22pm
v4
Comments
Maciej Los 11-Mar-18 5:27am    
It depends on the method you bind data...
Kim Gabinete 11-Mar-18 6:13am    
do you have an idea on how to code this? is it possible if i will get the index of the cell value i want on a selected row of first dgv? to put it on the 2nd dgv?
Maciej Los 11-Mar-18 7:43am    
how to code this? - as i mentioned, it depends... Can you tell me how you bind data to DataGridView? Use Improve question widget to update your question.
Kim Gabinete 12-Mar-18 2:23am    
i updated my question pls check it.

1 solution

Well, you didn't specify a method you use to bind data. Nevertheless...

The main thing is: separate of UI and data operations

Check this:
Copy datagridview row into another datagridview[^]
Copying one datagridview data to another datagridview in VB.NET[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900