vb.net 滚动到 Datagridview 选定的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31599956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:20:17  来源:igfitidea点击:

Scroll to Datagridview selected Row

vb.netwinformssearchdatagridviewrow

提问by Jesse James

I've done quite a bit of searching for this answer but none have been able to help me. I've attempted to use or even see if .Focus()was applicable since other websites suggested it, but it is not. I would just like the DataGridView, HistoryData.

我已经做了很多搜索这个答案,但没有人能够帮助我。我已经尝试使用,甚至查看是否.Focus()适用,因为其他网站建议使用它,但事实并非如此。我只想要DataGridView, HistoryData。

to jump to the selected row. It of course does so but it will not scroll to it when enough items fill the grid. Could there be a parameter i'm missing on the grid?

跳转到所选行。它当然会这样做,但是当足够的项目填充网格时它不会滚动到它。网格上是否有我遗漏的参数?

Here's my code:

这是我的代码:

    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

采纳答案by Reza Aghaei

If I understand your question correctly, You can scroll to specific row in DataGridViewusing either of these options:

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

CurrentCell

当前单元格

If you set the CurrentCellof DataGridViewit selects the specified cell and scrolls to make the cell visible.

如果您设置了它CurrentCellDataGridView它会选择指定的单元格并滚动以使该单元格可见。

For example to select the last row and scroll to it:

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

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

FirstDisplayedScrollingRowIndex

FirstDisplayedScrollingRowIndex

You can also set FirstDisplayedScrollingRowIndexto scroll to a specific row, but it doesn't select the row:

您还可以设置FirstDisplayedScrollingRowIndex滚动到特定行,但不会选择该行:

For example to only scroll to the 10th row:

例如只滚动到第 10 行:

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