繁体   English   中英

如何循环遍历datagridview 1并将循环结果以不同的形式和数据库表复制到datagridview2

[英]How to loop through datagridview 1 and copy loop results to datagridview2 in a different form and DB table

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

假设在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所有数据,然后将这些数据复制到Form2 DGV2

我真的需要这方面的帮助.. 任何提示或帮助将不胜感激。 谢谢,麻烦您了!

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

PS还在学习中

新问题但仍与原问题相关

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

我也可以以 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

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM