开发者

looping through datagridview

开发者 https://www.devze.com 2023-04-13 00:13 出处:网络
Mine is a windows app. containing forms named BOM nd BOMSelected.. There is datagridview in BOM which contains checkbox column.. When the user selects checkbox, the selected rows should be seen in th

Mine is a windows app. containing forms named BOM nd BOMSelected..

There is datagridview in BOM which contains checkbox column.. When the user selects checkbox, the selected rows should be seen in the datagridview of other form, SelectedBom..

I have coded but don't get it working.. Some error..

Can you please help ??

Your Help is greatly appreciated..

Here is what i have done !!

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


Try,

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

EDIT:

Demo:

Add Button1 and DataGridView1 in BOM form

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

Add DataGridView1 in SelectBOM form

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


Maybe instead of using the for each statement, you should instead use:

for istep as integer = 0 to datagridview.rowcount - 2

With the for each syntax, you can't assign a value to the individual row as you walk down the row.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号