【发布时间】:2012-11-26 07:23:33
【问题描述】:
我正在尝试对 Datagridview1(Table1) 中的值求和并将其保存到 Datagridview2(Table2)
DGVs 都是 Databound 并且都具有相同的 2 列 Name, Quantity。
在Childform1(DGV1) 上,我有一个Textbox1 和一个Post 按钮。
用户必须在Textbox1 上输入Name 并单击Post,然后必须将DGV1 上的所有Quantity 与Textbox1 上输入的Name 相加。
例如:
表 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
【问题讨论】:
-
这是我到目前为止所做的,它现在添加并保存到数据库问题是当我关闭我的 Childform2(DGV2) 时,我收到一个错误 No row can be added to a DataGridView control that没有列。必须先添加列。
-
有机会我会去看看的,应该不会有太大问题来解决
-
代码在 ChildForm.Closing 子中的外观如何?代码如何查看您打开 ChildForm 的位置?是使用 ShowDialog 还是 Show?你能提供这个代码吗?我们无法在似乎与问题无关的部分代码中找到错误:/
-
@KreepN 非常感谢....
-
@WozzeC 我用这 2 行打开 Childform2 `oChildForm2.MdiParent = MasterForm` 和
oChildForm2.Show()
标签: .net mysql vb.net winforms linq