滚动到 Datagridview 选定的行

Scroll to Datagridview selected Row

我搜索了很多这个答案,但 none 对我有所帮助。我曾尝试使用甚至查看 .Focus() 是否适用,因为其他网站建议这样做,但事实并非如此。我只想要 DataGridView、HistoryData.

跳转到所选行。它当然会这样做,但是当足够多的项目填满网格时它不会滚动到它。网格上可能缺少我的参数吗?

这是我的代码:

    Private Sub HistorySearch_TextChanged(sender As Object, e As EventArgs) Handles HistorySearch.TextChanged
    Try
        If HistorySearch.Text.ToString <> "" Then
            For Each HistoryRow As DataGridViewRow In HistoryData.Rows
                HistoryData.ClearSelection()
                For Each HistoryCell As DataGridViewCell In HistoryRow.Cells

                    If HistoryCell.Value.ToString.StartsWith(HistorySearch.Text.ToString) Then
                        HistoryRow.Selected = True
                        Dim i As Integer = HistoryData.CurrentRow.Index()

                    Else
                        HistoryRow.Selected = False
                    End If
                    If HistoryCell.Value.ToString.Contains(HistorySearch.Text.ToString) Then
                        HistoryRow.Selected = True
                        Dim i As Integer = HistoryData.CurrentRow.Index()
                        Return
                    Else
                        HistoryRow.Selected = False
                    End If
                Next
            Next
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

如果我正确理解你的问题,你可以使用以下任一选项滚动到 DataGridView 中的特定行:

CurrentCell

如果您设置 DataGridViewCurrentCell 它 select 指定的单元格并滚动以使单元格可见。

例如 select 最后一行并滚动到它:

'use suitable index, 10 is just for example
DataGridView1.CurrentCell = dataGridView1.Rows(10).Cells(0)

FirstDisplayedScrollingRowIndex

您还可以设置 FirstDisplayedScrollingRowIndex 滚动到特定行,但它不会 select 行:

例如只滚动到第 10 行:

'use suitable index, 10 is just for example
DataGridView1.FirstDisplayedScrollingRowIndex = 10