Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8907019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:47:55+00:00 2026-06-15T02:47:55+00:00

I am trying to sum the values from Datagridview1(Table1) and save it to Datagridview2(Table2)

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:47:56+00:00Added an answer on June 15, 2026 at 2:47 am

    Check the Posted column when iterating through your rows to see if a row has been posted or not:

    For Each xRows In MasterForm.oTransferRows.Where(Function(x) x.Cells("Posted").Value.ToString() = "No")
    

    Then use the following psuedo-logic to write your code:

     'code to select data - select name from testing where name = 'Product1'
     'if(dr.Read())
     'update goes here
     'Else
     ' insert goes here
     'End if
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm trying to sum up values from one record (A) then add it to
I'm trying sum many values from radio buttons, but I'm with some problems in
I'm trying to sum all the values from key[0] per weekday (key[1]). The final
I am trying to sum the values from 3 textboxes with the following statement,
I'm trying to update a table based on the sum of values from another
I'm having some difficulties creating a sum from values I have in a collection.
This is driving me bananas :) I am trying to SUM values from a
i'm trying to sum some values in a form, i have not managed to
Evening all: I am trying to sum number values from a column based off
Im trying to do a sum of values i get from id but it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.