Alex Luthor
Alex Luthor

Reputation: 151

How to loop through datagridview 1 and copy loop results to datagridview2 in a different form and DB table

I need help with this, I'll try to explain it in detail as much as I can.

Let's say in Form1 I have a Datagridview1 (DGV1) which is DataBound to Table1 with the columns TransactionNumber(Double), FormName (varchar), Description(varchar), Posted(text).

In Form2, I have another DGV2 which is DataBound to Table2 with the columns TransactionNumber(Double), Formname(VarChar), Description(VarChar), Quantity(Double).

In Form1 I have Textboxes to add data to the Columns in DGV1 and 2 Buttons Add and Post. When I click Post I want to loop through DGV1 and find all the data with the given TransactionNumber, then copy those data to DGV2 in Form2.

I really need help with this.. Any kind of tips or help would be greatly appreciated. Please and Thank You!

I still don't have codes for the Button Post as I'm still trying to figure out how to do this... I'm going to update this post with codes asap..

P.S. Still Learning

NEW QUESTION BUT STILL RELATED TO THE ORIGINAL QUESTION

I tweaked your code, it now adds data.

Could I also use this in an mdi form?

Dim occurences As New Dictionary(Of String, Double)

For Each DGVR As DataGridViewRow In Datagridview1.Rows

    If (Not DGVR.IsNewRow) Then

        If (occurences.ContainsKey(DGVR.Cells(1).Value.ToString())) Then

            occurences(DGVR.Cells(1).Value.ToString()) = Double.Parse(occurences(DGVR.Cells(1).Value.ToString()).ToString()) + Double.Parse(DGVR.Cells(4).Value.ToString())

        Else

            occurences.Add(DGVR.Cells(1).Value.ToString(), Double.Parse(DGVR.Cells(4).Value.ToString()))
        End If

    End If

Next


For Each KVP As KeyValuePair(Of String, Double) In occurences


    DataGridView2.Rows.Add(New Object() {KVP.Key, KVP.Value})

Next

Upvotes: 1

Views: 1228

Answers (1)

KreepN
KreepN

Reputation: 8598

Don't hate on me for this, as it is the best I could do in such a short time span:

http://www.fileswap.com/dl/KusycS0QTC/

Basically it's a project with an MDI parent form and two child forms. I have a DGV on each and I transfer the info from one form to the other. You will have to make the necessary edits to account for your setup, but it should be enough to give you an idea of how to go about what you're after.

EDIT:

Possible Changes:

     Dim _Name As String = ""
     Dim _Last As String = ""

      For Each xRow In MasterForm.oTransferRows
            _Name = xRow.Cells("GVName").Value.ToString()
            _Last = xRow.Cells("GVLast").Value.ToString()

'Should the next line be insert into?

            Dim _sqlInsert As String = String.Format("Insert testing(Name, LastName) Values  (@iName, @iLast)")
            Using conn As New SqlClient.SqlConnection("Server = localhost; Username= root; Password =; Database = test")
                Using cmd
                    With cmd
                        MsgBox("Connection Established")
                        .Connection = conn
                        .Parameters.Clear()
                        'Create Insert Query
                        .CommandText = _sqlInsert

                        .Parameters.Add(New SqlParameter("@iName", _Name))
                        .Parameters.Add(New SqlParameter("@iLast", _Last))
                    End With
                    Try
                        conn.Open()
                        Me.Validate()
                        cmd.ExecuteNonQuery()
                    Catch ex As Exception
                        MsgBox(ex.Message.ToString())
                    End Try
                End Using
            End Using

        Next

enter image description here

Upvotes: 2

Related Questions