6

我正在尝试使用 For... Next 循环检查 DataGridView 中的多个选择,但即使我选择了多行,唯一具有属性 Selected=True 的行是选择中的第一行。有没有解决的办法?

MultiSelect 在 DataGridView 上为 true。

我的代码如下:

For Each dr As DataGridViewRow In dgv.Rows
    If dr.Selected = True Then
        intSelectedRow = dr.Index
        SetTime("KeyEntry", dgv.Name, intSelectedRow)
    End If
Next

谢谢

4

2 回答 2

7

试试这个:

Dim selectedItems As DataGridViewSelectedRowCollection = dgv.SelectedRows
      For Each selectedItem As DataGridViewRow In selectedItems
            'Add code to handle whatever you want for each row
      Next
End Sub
于 2013-01-28T14:10:47.047 回答
-1
    Dim Message As String = String.Empty
    Dim FNL As FinalRpt = New FinalRpt()

    For Each ItemRow As DataGridViewRow In DGVItems.Rows

        Dim ISSelected As Boolean = Convert.ToBoolean(ItemRow.Cells("MyChkBox").Value)

        If ISSelected Then

            Message &= Environment.NewLine

            Message &= ItemRow.Cells("I_ID").Value.ToString()

            Dim SelectedRow As Integer = DGVItems.Rows.GetRowCount(DataGridViewElementStates.Selected)

            Dim RPTItemsDA As OleDbDataAdapter
            Dim RPTItemsDS As DataSet

            Dim I As Integer

            For I = 0 To SelectedRow Step 1

                RPTItemsDA = New OleDbDataAdapter("Select Distinct * From stkrpt Where I_ID = " & DGVItems.SelectedRows(I).Index.ToString() & "", DBConnect)
                RPTItemsDS = New DataSet

                RPTItemsDA.Fill(RPTItemsDS, "stkrpt")

                FNL.DGVReport.DataSource = RPTItemsDS
                FNL.DGVReport.DataMember = "stkrpt"
            Next

            FNL.MdiParent = MDIParent1

            FNL.StartPosition = FormStartPosition.CenterScreen
            FNL.WindowState = FormWindowState.Maximized


            Me.Hide()
            FNL.Show()

            ISSelected = False

        End If
    Next

    MessageBox.Show(Message)
于 2017-10-06T08:06:18.937 回答