Autoscroll datagrid to current row

Posted on 2006-05-11
Last Modified: 2008-03-03

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.


Question by:crestsolutions
    LVL 5

    Assisted Solution


    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
    LVL 8

    Assisted Solution

    using .net 1.1 datagrid

    setting the .CurrentRowIndex property selects the row and scrolls to it if it's not currently visible
    LVL 28

    Accepted Solution

    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

    Author Comment

    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    I'm currently working for a company where I have to upgrade over 50 VB6 programs to VB.NET 2008.  So far I'm about half way through, and I've learned quite a few tricks that drastically improve the performance of VB.NET apps. Because there are a…
    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    This video discusses moving either the default database or any database to a new volume.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now