Question : VB.NET, 2005 - Maintain Scroll Position To DataGrid

Hi Experts,
I have a data grid that gets filled dynamically with each database update. So the grid gets filled record by record, and not all in bulk.

The problem, sometimes I want to view the latest, so I scroll down to the end of the grid, but when it refreshes, it holds focus of the last record that was on it and keeps the focus as the record goes up since other new records are being added below!

I want to have the focus on the last record all the time, or wherever I do have the position (top/bottom).

How do I do that?

Answer : VB.NET, 2005 - Maintain Scroll Position To DataGrid

This is working for me.  One form, one button, one datagridview - called dgv.  This code

Public Class Form1

    Private dt As New DataTable("Test")
    Private cm As CurrencyManager

    Private Sub maketable()
        Dim dc0 As New DataColumn("ID", GetType(Integer))
        dc0.AutoIncrement = True
        dt.Columns.Add(dc0)
        For i As Integer = 0 To 4
            Dim dr As DataRow = dt.NewRow
            dt.Rows.Add(dr)
        Next
        dt.AcceptChanges()
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        maketable()
        dgv.DataSource = dt
        cm = BindingContext(dt)

    End Sub

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim gotoend As Boolean = False
        Dim oldpos As Integer = cm.Position
        If oldpos = cm.Count - 1 Then
            gotoend = True
        End If
        Dim dr As DataRow = dt.NewRow
        dt.Rows.Add(dr)
        If gotoend Then
            cm.Position = cm.Count - 1
        End If
    End Sub
End Class

Roger
Random Solutions  
 
programming4us programming4us