Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll to Datagridview selected Row

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
like image 664
Jesse James Avatar asked Jul 23 '15 23:07

Jesse James


People also ask

How do I make the DataGridView show the selected row?

Just set the CurrentCell property, the DGV will scroll to make it visible.

What is DataGridView in C#?

The DataGridView control provides a customizable table for displaying data. The DataGridView class allows customization of cells, rows, columns, and borders through the use of properties such as DefaultCellStyle, ColumnHeadersDefaultCellStyle, CellBorderStyle, and GridColor.


1 Answers

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
like image 73
Reza Aghaei Avatar answered Nov 15 '22 07:11

Reza Aghaei