我使用文本框和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
,fullrowselect
设置为true
。当我手动点击一行或用键盘选择它时,一切都正确打印。
我做错了什么?
答案 0 :(得分: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
P.S。我没有检查下一个可见单元格的代码,但看起来没问题,行选择正常