通过循环将特定单元格值从多个dgv复制到dgv的所有行中的特定单元格

时间:2016-01-16 18:41:19

标签: vb.net

我有两种形式:form2与dgv,form3与tabcontrol有一些标签页,每个标签页插入一个dgv。我想得到form2 dgv中每行的cell(1)值等于form3的多个dgv中row(13)的cell(1)的值,以便:

 DataGridView1.Rows(0).Cells(1).Value = Form3.DataGridView1.Rows(13).Cells(1).Value
    DataGridView1.Rows(1).Cells(1).Value = Form3.DataGridView2.Rows(13).Cells(1).Value
    DataGridView1.Rows(2).Cells(1).Value = Form3.DataGridView3.Rows(13).Cells(1).Value, etc.

我正在尝试以下代码:

For i As Integer = 0 To DataGridView1.Rows.Count - 1        'dgv in form2

        For Each tp As TabPage In Form3.TabControl1.Controls.OfType(Of TabPage)()
            For Each dgv As DataGridView In tp.Controls.OfType(Of DataGridView)()
                 DataGridView1.Rows(i).Cells(1).Value = dgv.Rows(13).Cells(1).Value
            Next
        Next
    Next

但结果是,只有form3 tabcontrol中的最后一个dgv显示了form2 dgv所有行的请求数据。我缺少什么,任何帮助高度赞赏。

1 个答案:

答案 0 :(得分:0)

这是因为你正在遍历for的内部的tabpages所以它总是落在i = 0,i = 1等的最后一个标签上

    Dim i As Integer = 0 
    For Each tp As TabPage In Form3.TabControl1.Controls.OfType(Of TabPage)()
        For Each dgv As DataGridView In tp.Controls.OfType(Of DataGridView)()
            If i < DataGridView1.Rows.Count Then
                DataGridView1.Rows(i).Cells(1).Value = dgv.Rows(13).Cells(1).Value
                1 += 1
            End If
        Next
     Next