0

我使用文本框和 DGV 构建了一个自动提示搜索。一切正常,除此之外,我确实想要 2 个按钮,它们提供向上或向下移动 1 行的功能。您必须知道,当我搜索时,我将那些不包含搜索字符串的行设置为.visible = false.

理论上它应该像这样工作:

Private Sub tsbDown_Click(sender As Object, e As EventArgs) Handles tsbDown.Click
    With dgvMA
       If dgvMA.RowCount > 0 Then
            Dim MyDesiredIndex As Integer = GetNextVisibleCell(dgvMA.CurrentRow.Index)

            dgvMA.ClearSelection()
            dgvMA.CurrentCell = dgvMA.Rows(MyDesiredIndex).Cells(1)
            dgvMA.Rows(MyDesiredIndex).Selected = True
        End If
    End With
End Sub

Private Function GetNextVisibleCell(currentrow As Integer) As Integer
    With dgvMA
        For i = currentrow To .Rows.Count - 1
            If .Rows(i).Visible = True Then
                MsgBox(i & ": " & .Rows(i).Visible)
                Return i
                Exit For
            End If
        Next

        Return currentrow
    End With
End Function

但永远不会选择我的行。multi-select设置为false和。当我手动单击一行或使用键盘选择它时,所有内容都会正确打印。fullrowselecttrue

我究竟做错了什么?

4

1 回答 1

0

这应该有效:

Private Sub tsbDown_Click(sender As Object, e As EventArgs) Handles tsbDown.Click
    With dgvMA
        If dgvMA.RowCount > 0 And dgvMA.CurrentRow.Index + 1 < dgvMA.Rows.Count Then
            Dim MyDesiredIndex As Integer = GetNextVisibleCell(dgvMA.CurrentRow.Index + 1)

            dgvMA.ClearSelection()
            dgvMA.CurrentCell = dgvMA.Rows(MyDesiredIndex).Cells(1)
            dgvMA.Rows(MyDesiredIndex).Selected = True
        End If
    End With
End Sub

Private Sub tsbUp_Click(sender As System.Object, e As System.EventArgs) Handles tsbUp.Click
    With dgvMA
        If dgvMA.RowCount > 0 And dgvMA.CurrentRow.Index <> 0 Then
            Dim MyDesiredIndex As Integer = GetNextVisibleCell(dgvMA.CurrentRow.Index - 1)

            dgvMA.ClearSelection()
            dgvMA.CurrentCell = dgvMA.Rows(MyDesiredIndex).Cells(1)
            dgvMA.Rows(MyDesiredIndex).Selected = True
        End If
    End With
End Sub

Private Function GetNextVisibleCell(currentrow As Integer) As Integer
    Dim i As Integer
    With dgvMA
        For i = currentrow To .Rows.Count - 1
            If .Rows(i).Visible = True Then
                ' MsgBox(i & ": " & .Rows(i).Visible)
                Return i
                Exit For
            End If
        Next

        Return currentrow
    End With
End Function

PS我没有检查获取下一个可见单元格的代码,但它似乎没问题,行选择工作正常

于 2013-08-01T08:42:56.503 回答