如何通过datagridview的1环和闭环结果复制到datagridview2以不同的形式和数据库表 [英] How to loop through datagridview 1 and copy loop results to datagridview2 in a different form and DB table

查看:114
本文介绍了如何通过datagridview的1环和闭环结果复制到datagridview2以不同的形式和数据库表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要这方面的帮助,我会尽力解释它的细节,就像我可以。

比方说,在 Form1中我有一个 dataGridView1的(DGV1)数据绑定表1 的列 TransactionNumber(双人间),窗体名称(VARCHAR),描述(VARCHAR),发布(文字)

窗体2 ,我还有一个 DGV2 数据绑定表2 的列 TransactionNumber(双人间),窗体名称(VARCHAR),描述(VARCHAR),数量(双人间)

Form1中文本框来将数据添加到列在 DGV1 和2个按钮添加和邮政业。当我点击发表我要遍历 DGV1 ,发现所有数据与给定的 TransactionNumber ,然后将这些数据复制到 DGV2 窗体2

我真的需要这方面的帮助。任何形式的提示或帮助将不胜AP preciated。请和谢谢!

我还没有codeS的按钮后,我还在努力弄清楚如何做到这一点...我会尽快更新这个职位与codeS ..

P.S。 仍然在学习

新问题,但仍涉及原题

我调整你的code,它现在增加了数据。

我还能在MDI的形式使用?

 昏暗的事件再度发生作为新词典(串,双人间)

对于每个DGVR作为的DataGridViewRow在Datagridview1.Rows

    如果(不DGVR.IsNewRow)然后

        如果(occurences.ContainsKey(DGVR.Cells(1).Value.ToString()))然后

            事件再度发生(DGVR.Cells(1).Value.ToString())= Double.Parse(点(DGVR.Cells(1).Value.ToString())。的ToString())+ Double.Parse(DGVR.Cells(4- ).Value.ToString())

        其他

            occurences.Add(DGVR.Cells(1).Value.ToString(),Double.Parse(DGVR.Cells(4).Value.ToString()))
        结束如果

    结束如果

下一个


对于每个KVP作为KeyValuePair(串,双)在事件再度发生


    DataGridView2.Rows.Add(新的对象(){KVP.Key,KVP.Value})

下一个
 

解决方案

不要恨上我要这个,因为它是最好的,我可以做在这么短的时间跨度:

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

基本上它与MDI父窗体和两个子窗体的项目。我有一个DGV每个我从一种形式传送信息到另一个。你将不得不作出必要的修改,以考虑您的设置,但它应该足以给你如何去你后的想法。

编辑:

可能的更改:

 昏暗_Name的String =
     _Last暗淡作为字符串=

      对于每个xRow在MasterForm.oTransferRows
            _name = xRow.Cells(GVName)。Value.ToString()
            _last = xRow.Cells(GVLast)。Value.ToString()
 

如果下一行被插入?

 昏暗_sqlInsert的String =的String.Format(插入测试(姓名,姓氏)值(@iName,@iLast))
            使用康恩作为新SqlClient.SqlConnection(服务器=本地主机;用户名=根;密码=;数据库=测试)
                使用CMD
                    用CMD
                        MSGBOX(连接已建立)
                        ■连接=康恩
                        .Parameters.Clear()
                        创建插入查询
                        .CommandText = _sqlInsert

                        .Parameters.Add(新的SqlParameter(@ INAME,_Name))
                        .Parameters.Add(新的SqlParameter(@ iLa​​st,_Last))
                    结束与
                    尝试
                        conn.Open()
                        Me.Validate()
                        cmd.ExecuteNonQuery()
                    抓住EX为例外
                        MSGBOX(ex.Message.ToString())
                    结束尝试
                结束使用
            结束使用

        下一个
 


I need help with this, I'll try to explain it in detail as much as I can.

Let's say in Form1 I have a Datagridview1 (DGV1) which is DataBound to Table1 with the columns TransactionNumber(Double), FormName (varchar), Description(varchar), Posted(text).

In Form2, I have another DGV2 which is DataBound to Table2 with the columns TransactionNumber(Double), Formname(VarChar), Description(VarChar), Quantity(Double).

In Form1 I have Textboxes to add data to the Columns in DGV1 and 2 Buttons Add and Post. When I click Post I want to loop through DGV1 and find all the data with the given TransactionNumber, then copy those data to DGV2 in Form2.

I really need help with this.. Any kind of tips or help would be greatly appreciated. Please and Thank You!

I still don't have codes for the Button Post as I'm still trying to figure out how to do this... I'm going to update this post with codes asap..

P.S. Still Learning

NEW QUESTION BUT STILL RELATED TO THE ORIGINAL QUESTION

I tweaked your code, it now adds data.

Could I also use this in an mdi form?

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

解决方案

Don't hate on me for this, as it is the best I could do in such a short time span:

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

Basically it's a project with an MDI parent form and two child forms. I have a DGV on each and I transfer the info from one form to the other. You will have to make the necessary edits to account for your setup, but it should be enough to give you an idea of how to go about what you're after.

EDIT:

Possible Changes:

     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()

'Should the next line be insert into?

            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


这篇关于如何通过datagridview的1环和闭环结果复制到datagridview2以不同的形式和数据库表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆