循环遍历datagridview

Ruc*_*i22 5 vb.net datagridview

我的是一个Windows应用程序。包含名为 BOM 和 BOMSelected 的表单..

BOM中有一个datagridview,其中包含checkbox列。当用户选择checkbox时,所选行应该在其他形式的datagridview中看到,SelectedBom。

我已经编码但无法正常工作..出现一些错误..

你能帮忙吗?

非常感谢您的帮助..

这就是我所做的!

Public Class SelectedBom

    Private Sub SelectedBom_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'HemDatabase1DataSet4.partno' table. You can move, or remove it, as needed.
        'Me.PartnoTableAdapter.Fill(Me.HemDatabase1DataSet4.partno)

        Dim count As Integer = 0

        For j As Integer = 0 To BOM.dgv1.RowCount - 1

            If BOM.dgv1.Rows(j).Cells(0).Value = True Then

                Dim ro As New DataGridViewRow
                DataGridView2.Rows.Add(ro)

                For i As Integer = 0 To BOM.dgv1.ColumnCount - 1
                    Me.DataGridView2.Rows(count).Cells(i).Value = BOM.dgv1.Rows(j).Cells(i).Value
                Next

                count += 1

            End If

        Next

    End Sub
End Class
Run Code Online (Sandbox Code Playgroud)

ada*_*ost 3

尝试,

For Each row As DataGridViewRow In BOM.dgv1.Rows
  Dim obj(row.Cells.Count - 1) As Object
  For i = 0 To row.Cells.Count - 1
      obj(i) = row.Cells(i).Value
  Next
  Me.DataGridView2.Rows.Add(obj)
Next
Run Code Online (Sandbox Code Playgroud)

编辑:

演示:

以BOM形式添加Button1和DataGridView1

Public Class BOM
    Public Class Sample
        Public Property Satus As Boolean
        Public Property Name As String
        Public Property ID As Integer
    End Class
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        SelectedBom.Show()
    End Sub
    Private Sub BOM_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim myList As New List(Of Sample)
        myList.Add(New Sample() With {.ID = 1, .Name = "A"})
        myList.Add(New Sample() With {.ID = 2, .Name = "B"})
        myList.Add(New Sample() With {.ID = 3, .Name = "C"})
        DataGridView1.DataSource = myList
    End Sub
End Class
Run Code Online (Sandbox Code Playgroud)

在SelectBOM表单中添加DataGridView1

Public Class SelectedBom

    Private Sub SelectedBom_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim i As Integer = 0
        DataGridView1.AutoGenerateColumns = False
        DataGridView1.Columns.Add("Name", "Name")
        DataGridView1.Columns.Add("No", "No")
        For Each row As DataGridViewRow In BOM.DataGridView1.Rows

            If DirectCast(row.Cells(0).Value, Boolean) Then
                DataGridView1.Rows.Add(row.Cells(1).Value, row.Cells(2).Value)
            End If
        Next
    End Sub
End Class
Run Code Online (Sandbox Code Playgroud)