.net - 如何循环遍历datagridview 1并将循环结果以不同的形式和DB表复制到datagridview2

标签 .net mysql vb.net winforms datagridview

我需要这方面的帮助,我会尽可能详细地解释它。

假设在 Form1 中,我有一个 Datagridview1 (DGV1),它是 DataBoundTable1 的列TransactionNumber(Double)、FormName (varchar)、Description(varchar)、Posted(text)

Form2 中,我有另一个 DGV2,它是 DataBoundTable2 的列 TransactionNumber (Double), Formname(VarChar), Description(VarChar), Quantity(Double).

Form1 中,我有 Textboxes 用于将数据添加到 DGV1 中的列和 2 个按钮 Add and Post .当我单击 Post 时,我想遍历 DGV1 并找到具有给定 TransactionNumber 的所有数据,然后将这些数据复制到 DGV2 Form2 中。

我真的需要帮助。任何类型的提示或帮助将不胜感激。请谢谢!

我仍然没有 Button Post 的代码,因为我仍在尝试弄清楚如何做到这一点......我将尽快用代码更新这篇文章......

附言 还在学习

新问题但仍与原始问题相关

我调整了您的代码,它现在添加了数据。

我也可以在 mdi 形式中使用它吗?

Dim occurences As New Dictionary(Of String, Double)

For Each DGVR As DataGridViewRow In Datagridview1.Rows

    If (Not DGVR.IsNewRow) Then

        If (occurences.ContainsKey(DGVR.Cells(1).Value.ToString())) Then

            occurences(DGVR.Cells(1).Value.ToString()) = Double.Parse(occurences(DGVR.Cells(1).Value.ToString()).ToString()) + Double.Parse(DGVR.Cells(4).Value.ToString())

        Else

            occurences.Add(DGVR.Cells(1).Value.ToString(), Double.Parse(DGVR.Cells(4).Value.ToString()))
        End If

    End If

Next


For Each KVP As KeyValuePair(Of String, Double) In occurences


    DataGridView2.Rows.Add(New Object() {KVP.Key, KVP.Value})

Next

最佳答案

不要因为这个而恨我,因为这是我在这么短的时间内能做的最好的事情:

http://www.fileswap.com/dl/KusycS0QTC/

基本上它是一个具有 MDI 父窗体和两个子窗体的项目。我每个人都有一个 DGV,我将信息从一种形式转移到另一种形式。您将必须进行必要的编辑以说明您的设置,但这应该足以让您了解如何进行您所追求的。

编辑:

可能的变化:

     Dim _Name As String = ""
     Dim _Last As String = ""

      For Each xRow In MasterForm.oTransferRows
            _Name = xRow.Cells("GVName").Value.ToString()
            _Last = xRow.Cells("GVLast").Value.ToString()

'是否应该插入下一行?

            Dim _sqlInsert As String = String.Format("Insert testing(Name, LastName) Values  (@iName, @iLast)")
            Using conn As New SqlClient.SqlConnection("Server = localhost; Username= root; Password =; Database = test")
                Using cmd
                    With cmd
                        MsgBox("Connection Established")
                        .Connection = conn
                        .Parameters.Clear()
                        'Create Insert Query
                        .CommandText = _sqlInsert

                        .Parameters.Add(New SqlParameter("@iName", _Name))
                        .Parameters.Add(New SqlParameter("@iLast", _Last))
                    End With
                    Try
                        conn.Open()
                        Me.Validate()
                        cmd.ExecuteNonQuery()
                    Catch ex As Exception
                        MsgBox(ex.Message.ToString())
                    End Try
                End Using
            End Using

        Next

enter image description here

关于.net - 如何循环遍历datagridview 1并将循环结果以不同的形式和DB表复制到datagridview2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13466028/

相关文章:

c# - Enumerable.Range 的高内存消耗?

mysql - 如何从另一个表和用户输入插入到一个表中?

php - PHP 与 MySQL 的执行时间

c# - 对列表中的一系列整数求和

c# - 从 SHA1 迁移到 SHA2 ASP.net 4.5,C#

c# - 为什么 int 数组的最大大小小于 Int32.MaxValue?

.net - 通过访问器或直接访问同一类的属性的最佳方法是什么?

Mysql:按字符串分组的最新项目

.net - 比较两个列表以获取同时出现在两个列表中的对象

.net - 如何检测消息框是否可见?