如何将两个datagrid视图(dt1和dt2)行值添加到vb.net中的新数据网格视图dt3

时间:2016-11-17 09:09:39

标签: vb.net

我有两个datagridview表: dt1 dt2 。我想将值 dt1 dt2 组合在一起,并将组合值合并到新的datagridview dt3

如果 dt1 的第一个值为0000且 dt2 为02,则 dt3 中的值应为0000,02

按照我的代码:

For c = 0 To DataDataGridView1.Rows.Count - 1
     For t = 0 To DataDataGridView2.Rows.Count - 1
         DataGridView3.Rows.Add(DataDataGridView1.Rows(c).Cells(0).Value)
     Next
Next

1 个答案:

答案 0 :(得分:0)

这应该是你所追求的。由于我坚持使用默认值,因此名称略有不同。

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    DataGridView1.Rows.Add("0000")
    DataGridView1.Rows.Add("0001")
    DataGridView1.Rows.Add("0002")

    DataGridView2.Rows.Add("02")
    DataGridView2.Rows.Add("04")
    DataGridView2.Rows.Add("06")

    For c = 0 To DataGridView1.Rows.Count - 2 '2 because of the extra row used for editing
        For t = 0 To DataGridView2.Rows.Count - 2
            DataGridView3.Rows.Add(DataGridView1.Rows(c).Cells(0).Value.ToString & "," & DataGridView2.Rows(t).Cells(0).Value.ToString)
        Next
    Next

End Sub

输出如下:

Sub output