1

我有 1 个DataGridView,我想将所有记录转移到另一个DataGridView. 我怎么可能做到这一点?

dgvCourseList - 1st datagridview
dgvCourseHandled - 2nd datagridview

这是我将一条记录转移到另一条记录的代码DataGridView

Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
    If (chckCourse(dgvCourselist.SelectedCells.Item(0).Value)) = True And (chckDup(dgvCourselist.SelectedCells.Item(0).Value)) = False Then
        dgvCoursesHandled.Rows.Add(dgvCourselist.SelectedCells.Item(0).Value)
    End If
End Sub
4

3 回答 3

2
Private Sub btnInsertAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsertAll.Click

        If (chckCourse(dgvCourselist.SelectedCells.Item(0).Value)) = True And (chckDup(dgvCourselist.SelectedCells.Item(0).Value)) = False Then
            For i As Integer = 0 To Me.dgvCourselist.Rows.Count - 1
                dgvCoursesHandled.Rows.Add(dgvCourselist.Rows(i).Cells(0).Value)
            Next

        End If
    End Sub
于 2012-05-10T00:48:30.267 回答
1

你几乎在正确的轨道上。

if [check if row is selected from the dgvCourselist]


  dgvCourseHandled.Rows.Add(dgvCourseList.Cells("columnname").Value, _
                             repeat until all column in dgvCourseHandled are satisfied)
  1. 确定 dgvcoursehandled 中有多少列和列名。提示:如果您没有指定列名,请这样做,如果您有很多列,“magic no”将无法帮助您调试。例如 dgvCourseList.Cells("columnname").Value
  2. 识别 dgvcourselist 中将传递给 dgvcoursehandled 的所有列

当您确定所有这些时,请遵循上面的伪代码。

于 2012-05-09T08:34:25.253 回答
0

这个也可以试试。

首先复制列:

For Each col As DataGridViewColumn In DataGridView1.Columns
        DataGridView2.Columns.Add(DirectCast(col.Clone, DataGridViewColumn))
Next

然后数据

For Each row As DataGridViewRow In DataGridView1.Rows
    DataGridViewRow2.Rows.Add(row.Cells.Cast(Of DataGridViewCell).Select(Function(c) c.Value).ToArray)
Next

来源:http ://www.vbforums.com/showthread.php?678009-Copying-rows-and-columns-from-one-datagridview-to-another

于 2016-04-07T05:55:27.730 回答