• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 774
  • Last Modified:

Autoscroll datagrid to current row


Is it possible to get the datagrid in .net 1.1 to auto scroll to the current selected row programmically. At the moment I can select a specific row but if that row is near the end of the grid the user cannot see it without having to manually scroll down.


3 Solutions

with this code you can control the scrollbar of the datagrid, I have however no idea how you can make it scroll until the selected row is visible. I hope this'll get you on your way, good luck.

        Dim ctl As Control, scrollCtl As VScrollBar

        For Each ctl In DataGrid1.Controls
            If TypeOf (ctl) Is VScrollBar Then
                scrollCtl = ctl
                With scrollCtl
                    'do your thing
                End With
            End If
using .net 1.1 datagrid

setting the .CurrentRowIndex property selects the row and scrolls to it if it's not currently visible
Scroll to a specific row in a System.Windows.Forms.Datagrid

The following examples show how to set and get the current cell.

If the current cell is set to a cell which is not currently visible, the datagrid will scroll to that cell.

Sample Code:

Private Sub SetCellWithFocus(ByVal myGrid As DataGrid)
' Set the current cell to cell 1, row 1.
myGrid.CurrentCell = New DataGridCell(1,1)
End Sub

Private Sub DataGrid1_GotFocus(ByVal Sender As Object, ByVal e As EventArgs)
Console.WriteLine(DataGrid1.CurrentCell.ColumnNumber & " " & _
End Sub

'scroll to the new added row
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim dr As DataRow = ds.Tables(0).NewRow
        dr(0) = "Iouri"
        dg.CurrentCell = New DataGridCell(ds.Tables(0).Rows.Count - 1, 1)' (row,cell)
    End Sub
crestsolutionsAuthor Commented:
Found a simple solution on the net just a few moments ago.

The DataGrid has a protected GridVScrolled member that can be used to scroll the grid. To use it, you can derive from DataGrid and add a ScrollToRow method

'* automatically scroll to a certain row.
    Sub ScrollToRow(ByVal row As Integer)
        If Not Me.DataSource Is Nothing Then
                Me.GridVScrolled(Me, New ScrollEventArgs(ScrollEventType.LargeIncrement, row))
        End If
    End Sub


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now