1

からの値を合計してDatagridview1(Table1)保存しようとしていますDatagridview2(Table2)

両方ともDGVs同じDatabound2列Name, Quantityです。

にボタンChildform1(DGV1)Textbox1ありPostます。

ユーザーはonを入力しNameTextbox1クリックする必要があります。次に、で入力したものと同じPostすべてのQuantityonDGV1を合計する必要があります。NameTextbox1

元:

表1

Name | Quantity
 a       10
 b        5
 a       10
 b        5

POSTボタンを押した後

表2

Name | Quantity
 a        20
 b        10

コード

    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

DGV1このコードは、最初のクリックで同じ名前を合計し、に保存するだけDGV2です。

しかし、たとえばDGV1、同じ名前で別のデータを追加します。

新しく追加されたデータのQUANTITYを、DGV2の既存のデータの現在の合計に追加したいと思います。

これについての少しの助けも本当に素晴らしいでしょう。**

元:

表2

Name | Quantity
 a       20
 b       10

次に、表1にデータを追加します

表1

Name | Quantity
  a       20
  b       10

ポストを押した後

表2

Name | Quantity
 a        40
 b        20
4

1 に答える 1

1

行を繰り返し処理するときに、投稿された列をチェックして、行が投稿されたかどうかを確認します。

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

次に、次の疑似ロジックを使用してコードを記述します。

 'code to select data - select name from testing where name = 'Product1'
 'if(dr.Read())
 'update goes here
 'Else
 ' insert goes here
 'End if
于 2012-11-27T08:47:56.607 に答える