Scroll to Datagridview selected Row

16,326

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

CurrentCell

If you set the CurrentCell of DataGridView it selects the specified cell and scrolls to make the cell visible.

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

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

For example to only scroll to the 10th row:

'use suitable index, 10 is just for example
DataGridView1.FirstDisplayedScrollingRowIndex = 10
Share:
16,326
Admin
Author by

Admin

Updated on June 16, 2022

Comments

  • Admin
    Admin almost 2 years

    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.

    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