I am trying to sum the values from Datagridview1(Table1) and save it to Datagridview2(Table2)
both DGVs are Databound and both have the same 2 columns Name, Quantity.
On Childform1(DGV1) I have a Textbox1 and a Post button.
The user must input a Name on Textbox1 and click Post, then all the Quantity on DGV1 must be summed up that has the same Name as entered on Textbox1.
ex:
Table1
Name | Quantity
a 10
b 5
a 10
b 5
After pressing POST button
Table 2
Name | Quantity
a 20
b 10
Codes
Dim occurences As New Dictionary(Of String, Double)
Dim oTempObjects As New List(Of DataGridViewRow)
MasterForm.oTransferRows = oTempObjects
For Each xRow As DataGridViewRow In Datagridview1.Rows
If (Not xRow.Cells("GVName").Value Is Nothing AndAlso xRow.Cells("GVName").Value.ToString() = TextBox1.Text) Then
oTempObjects.Add(xRow)
End If
Next
Dim _Name As New List(Of String)
For Each xRows In MasterForm.oTransferRows
_Name.Add("'" & xRows.Cells("GVName").Value.ToString() & "'")
Dim inClause1 As String = String.Join(",", _Name.ToArray())
Dim _sqlUpdate As String = String.Format("UPDATE tested SET Posted = @Posted WHERE Name IN ({0})", inClause1)
Using _conn As New MySqlConnection("Server = localhost; Username= root; Password =; Database = test")
Using _commm As New MySqlCommand()
With _commm
.CommandText = _sqlUpdate
.Connection = _conn
.CommandType = CommandType.Text
.Parameters.AddWithValue("@Posted", "Yes")
End With
Try
_conn.Open()
_commm.ExecuteNonQuery()
Catch ex As MySqlException
MsgBox(ex.StackTrace.ToString)
End Try
_conn.Close()
End Using
End Using
If (occurences.ContainsKey(xRows.Cells(1).Value.ToString())) Then
occurences(xRows.Cells(1).Value.ToString()) = Double.Parse(occurences(xRows.Cells(1).Value.ToString()).ToString()) + Double.Parse(xRows.Cells(4).Value.ToString())
Else
occurences.Add(xRows.Cells(1).Value.ToString(), Double.Parse(xRows.Cells(4).Value.ToString()))
End If
Next
For Each KVP As KeyValuePair(Of String, Double) In occurences
oChildForm2.DataGridView2.Rows.Add(New Object() {KVP.Key, KVP.Value})
Next
Dim xlist As List(Of KeyValuePair(Of String, Double)) = occurences.ToList
For Each pair As KeyValuePair(Of String, Double) In occurences
Dim oUpdateString = String.Format("Insert Into testing (Name, Quantity) VALUES (@iName, @iQty)")
Using _conn As New MySqlConnection("Server = localhost; Username= root; Password =; Database = test")
Using _commm As New MySqlCommand()
With _commm
.CommandText = oUpdateString
.Connection = _conn
.CommandType = CommandType.Text
.Parameters.AddWithValue("@iName", pair.Key)
.Parameters.AddWithValue("@iQty", pair.Value)
End With
Try
_conn.Open()
_commm.ExecuteNonQuery()
Catch ex As MySqlException
MsgBox(ex.StackTrace.ToString)
End Try
_conn.Close()
End Using
End Using
Next
This code only sums the same Name on the DGV1 on the first click and saves it on DGV2.
but for example I add another data on DGV1 with the same name,
I want the QUANTITY of the newly added data to add to the current sum of the existing data on DGV2.
A little help with this too would be really awesome.**
Ex:
TABLE 2
Name | Quantity
a 20
b 10
Then I add data on Table 1
Table1
Name | Quantity
a 20
b 10
After Pressing Post
Table2
Name | Quantity
a 40
b 20
Check the Posted column when iterating through your rows to see if a row has been posted or not:
Then use the following psuedo-logic to write your code: