如何循环使用datagridview 1,并将循环结果复制到不同格式和DB表中的datagridview2 [英] How to loop through datagridview 1 and copy loop results to datagridview2 in a different form and DB table
问题描述
我需要帮助,我会尽可能详细地解释它。
I need help with this, I'll try to explain it in detail as much as I can.
让我们在 Form1
我有一个 Datagridview1(DGV1)
这是 DataBound
到 Table1
与列 TransactionNumber(Double),FormName(varchar),描述(varchar),发布(文本)
。
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)
.
在 Form2
中,我有另一个 DGV2
DataBound
to Table2
,其中列 TransactionNumber(Double),Formname(VarChar),Description VarChar),数量(Double)
。
In Form2
, I have another DGV2
which is DataBound
to Table2
with the columns TransactionNumber(Double), Formname(VarChar), Description(VarChar), Quantity(Double)
.
在 Form1
我有文本框
将数据添加到 DGV1
和2个按钮中的列添加和发布
。当我点击发布
我想循环通过 DGV1
并找到所有的数据与给定的 TransactionNumber
,然后将这些数据复制到 Form2
中的 DGV2
。
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!
我仍然没有按钮帖子的代码,因为我仍然想知道如何做到这一点...我是要更新这个帖子与代码asap ..
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..
PS
仍然学习
P.S. Still Learning
新问题,但仍然与原始问题相关
我调整了代码,现在添加了数据。
我也可以使用mdi形式吗?
$ b
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/
基本上它是一个带有MDI父表单和两个子表单的项目。我每个都有一个DGV,我将信息从一个表单转移到另一个。您必须进行必要的编辑来解决您的设置,但是应该足以让您了解如何处理您的问题。
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.
编辑:
可能的更改:
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
这篇关于如何循环使用datagridview 1,并将循环结果复制到不同格式和DB表中的datagridview2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!