locked
Why do datagridview resets its scrolling when datasource is updated RRS feed

  • Question

  • Hello everyone!

    Suppose I have an updater which once in a 2000 ms gets data from the server (e.g. data is dataset). The updater is a black box for me, one thing I know that it provides new data if this data somehow changed by other reasons.

    Suppose I have binding source and a datagridview with it bound to source. Source is having it .DataSource property set to an instance of the dataset which was created one time in constructor. So, when new updates come to me I just merge it into my dataset so user can see what changes have happened.

    Everything is going happy but except for one very-very annoying trouble - when user scrolls down the datagrid, it resets it's scroll after new portion of data is merged into very first and only instance of dataset.

    Why is that happening? Would be nice if someone could explain this issue and give some tips about avoiding this problem.

    Thanks!
    Tuesday, February 26, 2008 10:23 PM

Answers

  • The reason is that the datasource has changed so the grid defaults to the top. Remember you're not updating, you're refreshing.

     

    Adam

    Tuesday, February 26, 2008 10:25 PM

All replies

  • The reason is that the datasource has changed so the grid defaults to the top. Remember you're not updating, you're refreshing.

     

    Adam

    Tuesday, February 26, 2008 10:25 PM
  • Adam, could you explain on fingers whats the difference between "refresing" and "updating" in my case?

    Thanks.
    Tuesday, February 26, 2008 10:27 PM
  • Updating = keeps the ordered index

    Refreshing = loads a new dataset - flushes the index

     

    Adam

    Tuesday, February 26, 2008 10:38 PM
  • Adam, I still don't get your point.

    Look.
    I have a bound datatable with ummm 10 records.

    Then update comes and i see that new incoming table has 2 records, one record is new and other is an update.

    So I merge new table into the the old one and... tadaam, I have my table filled with 11 records - means I have one new and one of the others changed it's field.

    Scroll is reset too, oh that's a pity.

    Ok. You tell me I'm doing a refresh here.

    What should I do to make an update, so that index wont be reset?
    Thanks!
    Wednesday, February 27, 2008 2:54 PM
  • Oh please help, anyone! Smile
    Thursday, February 28, 2008 10:42 AM
  • Here's the full code I testing with both scroll bars:

     

    Code Snippet

    Imports System.Data.OleDb

    Public Class Form1

     

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

       Me.CustomersTableAdapter.Fill(Me.NorthwindDataSet.Customers)

       Timer1.Start()

    End Sub

     

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

       MaintainScrollPosition.GetvSBarPos(DataGridView1)

       MaintainScrollPosition.GethSBarPos(DataGridView1)

       Using cn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Northwind.mdb")

          Dim query As String = "Select * from Customers"

          Dim ds As New DataSet

          Dim da As OleDbDataAdapter = New OleDbDataAdapter(query, cn)

          da.Fill(ds, 0)

          DataGridView1.DataSource = ds.Tables(0).DefaultView

       End Using

       MaintainScrollPosition.SetvSBarPos(DataGridView1)

       MaintainScrollPosition.SethSBarPos(DataGridView1)

    End Sub

    End Class

     

    Public Class MaintainScrollPosition

       Private Shared vposition As Integer

       Private Shared hposition As Integer

     

       Shared Sub GetvSBarPos(ByVal dg As DataGridView)

           vposition = dg.FirstDisplayedScrollingRowIndex

       End Sub

     

       Shared Sub SetvSBarPos(ByVal dg As DataGridView)

           dg.FirstDisplayedScrollingRowIndex() = vposition

       End Sub

     

       Shared Sub GethSBarPos(ByVal dg As DataGridView)

          hposition = dg.FirstDisplayedScrollingColumnIndex

       End Sub

     

       Shared Sub SethSBarPos(ByVal dg As DataGridView)

          dg.FirstDisplayedScrollingColumnIndex = hposition

       End Sub

    End Class

     

     

     

    Adam

    Thursday, February 28, 2008 10:05 PM