vb.net 循环遍历数据网格视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7777205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:44:11  来源:igfitidea点击:

looping through datagridview

vb.netdatagridview

提问by Ruchi22

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

我的是一个Windows应用程序。包含名为 BOM 和 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..

BOM中有包含复选框列的datagridview..当用户选择复选框时,所选行应该在其他表单的datagridview中看到,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

回答by adatapost

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

在 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

Add DataGridView1 in SelectBOM form

在 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

回答by Trebuchet

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

也许不是使用 for each 语句,您应该使用:

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.

使用 for each 语法,您不能在沿着行向下走时为单个行分配值。