滚动到Datagridview选定的行

问题描述 投票:5回答:1

我已经做了很多寻找此答案的工作,但是没有一个能够帮助我。自其他网站建议以来,我尝试使用甚至查看.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
vb.net winforms search datagridview row
1个回答
5
投票

如果我正确理解了您的问题,则可以使用以下两个选项之一滚动到DataGridView中的特定行:

CurrentCell

如果设置CurrentCellCurrentCell,它将选择指定的单元格并滚动以使该单元格可见。

例如,选择最后一行并滚动到它:

DataGridView

FirstDisplayedScrollingRowIndex

您也可以将'use suitable index, 10 is just for example DataGridView1.CurrentCell = dataGridView1.Rows(10).Cells(0) 设置为滚动到特定行,但不会选择该行:

例如,仅滚动到第十行:

FirstDisplayedScrollingRowIndex
© www.soinside.com 2019 - 2024. All rights reserved.