Best practices for syncronizing grid view

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for MySQL
Post Reply
Posts: 38
Joined: Tue 07 Mar 2006 17:13

Best practices for syncronizing grid view

Post by » Thu 17 May 2007 13:38

I know this is not a Corelab issue or problem, but I was curious to know if anybody might be able to offer any suggestions in this forum:

I have a simple application which fetches data from a data table and displays it to users via a GridView for Read-only purposes. The code to load the form is fairly straightforward:

Dim myTable as new MySqlDataTable("Select * from dbTable order by xxx",connDBTable)
myTable.Active = True
myGridView.DataSource = myTable

Every 5 seconds I have a timer event fire which reloads the data as follows, so that if the data has changed on the server the display is updated:

myTable.Active = False
myTable.Active = True
myGridView.DataSource = myTable

My goal is to have the data displayed update, but I don't want the GridView to "reset" back to the top of the control. I've tried writing code that resets the selected or focused row back to the same position as before the update, but it doesn't do everything I want.

As a second solution, I'm trying to take advantage of the DataTable.Merge command, whereby I fetch the original table, make a copy, bind the copy to the GridView, and then merge the original table into the copy on each successive timer event.

It works for the most part, its fast enough for my purposes, but it has one major drawback: If a row which was originally in my server-side database is deleted, the "orphan" in my copy stays present on successive merges. It seems that the merge command will update existing rows, and add new rows, but has no mechanism for knowing that the original row is no longer there and deleting it.

Does anybody know of a way to make the merge handle this scenario? Or perhaps does anybody know of a better way to try to synchronize a read-only table to a server-side datatable in such a way as to not require a total destruction and reload of the datable bound to the grid control?

Any thoughts would be appreciated...

Many thanks,


Posts: 2756
Joined: Mon 13 Mar 2006 07:43

Post by Alexey » Fri 18 May 2007 09:01

Doesn't the following code suit your needs:

Code: Select all

            int row = dataGridView1.FirstDisplayedCell.RowIndex;
            int col = dataGridView1.FirstDisplayedCell.ColumnIndex;
            mySqlDataTable1.Active = false;
            mySqlDataTable1.Active = true;
            dataGridView1.DataSource = mySqlDataTable1;
            dataGridView1.FirstDisplayedScrollingRowIndex = row;
            dataGridView1.FirstDisplayedScrollingColumnIndex = col;

Post Reply