将Datagridview1(Table1)中的值相加并使用Linq和MySql将其保存到Datagridview2(Table2)

时间:2012-11-26 07:23:34

标签: .net mysql vb.net winforms linq

我正在尝试将Datagridview1(Table1)中的值相加并将其保存到Datagridview2(Table2)

DGVs Databound都是Name, Quantity,两者都有相同的2列Childform1(DGV1)

Textbox1上我有一个Post和一个Name按钮。

用户必须在Textbox1上输入Post并点击Quantity,然后DGV1Name上的所有Textbox1必须总结为Name | Quantity a 10 b 5 a 10 b 5 {已在POST上输入{1}}。

例如:

表1

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 按钮

表2

DGV1

DGV2

此代码仅在第一次点击时将DGV1上的相同名称相加,并将其保存在Name | Quantity a 20 b 10 上。

但是例如我在Name | Quantity a 20 b 10 上添加了同名的另一个数据

我希望新添加数据的QUANTITY添加到DGV2上现有数据的当前总和。

对此也有一点帮助真的很棒。**

例如:

表2

Name | Quantity
 a        40
 b        20

然后我在表1中添加数据

表1

{{1}}

按后发帖

表2

{{1}}

1 个答案:

答案 0 :(得分: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