将datagidview中的值复制到不同表单上的另一个datagidview

时间:2017-10-14 16:22:51

标签: vb.net datagridview

我有一个带有DataGridView的主窗体(FormXYZ),它包含XYZ坐标和点的名称和描述。我正在使用MenuStrip使用DGV中的选定点来调用事件。

当我调用该事件时,它将打开一个新表单(Form4),其中包含该函数的相关参数。我想在这个新表格中加入一个新的DGV,其中包括基础DGV上的所有点。

以下是我到目前为止的代码,该代码已经从我在该主题上找到的其他帖子改编而来。它似乎运行没有错误,但新的DGV没有显示任何数据。此代码描述了它将如何基于基本DGV创建列。我不需要这个,因为我的DGV都包含相同的列,但是我已经把它留在了这个阶段,直到我可以传输一些数据。

提前感谢您的帮助。

Public Class FormTranslate 
  Private Sub FormTranslate_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim sourceGrid As DataGridView = FormXYZ.DataGridView1
    Dim targetGrid As DataGridView = Me.DGV1

    '----------
    'Copy all rows and cells.
    Dim targetRows = New List(Of DataGridViewRow)

    For Each sourceRow As DataGridViewRow In sourceGrid.Rows
        If (Not sourceRow.IsNewRow) Then
            Dim targetRow = CType(sourceRow.Clone(), DataGridViewRow)
            For Each cell As DataGridViewCell In sourceRow.Cells
                targetRow.Cells(cell.ColumnIndex).Value = cell.Value
            Next
            targetRows.Add(targetRow)
        End If
    Next

    'Clear target columns and then clone all source columns.
    targetGrid.Columns.Clear()

    For Each column As DataGridViewColumn In sourceGrid.Columns
        targetGrid.Columns.Add(CType(column.Clone(), DataGridViewColumn))
    Next

    'It's recommended to use the AddRange method (if available)
    'when adding multiple items to a collection.
    targetGrid.Rows.AddRange(targetRows.ToArray())
  End Sub
End Class

0 个答案:

没有答案
相关问题