Reload the grid and still keep the current selected item

pisceswzh

Well-known member
Joined
Mar 19, 2007
Messages
96
Programming Experience
1-3
Hi, there,

I have a problem with the grid control.

I have a grid control on a windows form and I fill this grid with a table.

And now I want to periodically update the data in this table from the database, since a bunch of users are using this application, so if it doesn't automatically update, the data in the grid may not be the data in the database.

It will be easy if I just reload the grid, but everytime I reload it, the user's current selection of a row of the grid will jump off. And I don't want this happen.

Any solution? Thanks!
 
Simple. BEFORE you reload just remember which record was selected, then select it again AFTER you reload.

I am using the sqlDataAdapter to Fill a DataTable and the bind the grid control to the DataTable.


If I sqlDataAdapter.Fill again at this scenario, the data in the grid is just loaded repeatedly, not update the original data in the grid. Like

|1000|Apple| 'This is displayed when the first time I fill the DataTable
|1001|Banana|
|1000|Apple| 'This is displayed when the second time I fill the DataTable
|1001|Banana|

==================================

If I use DataTable.Clear before sqlDataAdapter.Fill, then the row the user selected before any updates happen is just gone after the data is updated although the displayed data is the same. Like

'This is the grid with filled data, and we assume that the user selects the Banana row now. And of course I can save this row into a DataRow object.
|1000|Apple|
|1001|Banana|

'But if now, I DataTable.Clear and sqlDataAdapter.Fill again, although the dataTable looks just the same as before, but the banana row is no longer the row that I have saved before. Thus I will not be able to re-select the row that I have saved.
 
How can I just reload the DataTable and keep the original values in it? I mean just changes those values that have been changed in the database or new in the database and remove those that have been removed in the database.
 
Reload the DataGrid

Hi, there

I am just wondering whether it is possible to reload a DataGrid without affecting the user current selection?

For the time moment, the way I reload a DataGrid is like

MyGird.DataSource = MyDataTable
MyDataTable.Clear
MyDataAdapter.Fill(MyDataTable)

But if I do so, everytime the sub is run, the user current selection is jump to the first row in the grid and I don't want this happen. Any idea?
 
Actually, in my project, I want to periodically reload the DataGrid from the database. Since a lot of users are using it, if I don't do so, the data in the DataGrid on you screen may not be the data in the database.

Anybody knows what is the industrial solution for this issue?
 
place a timer?? and refresh the dataset /
 
Look at your data. How do you identify a row? By its ID of course. If the row with an ID of 1000 is select BEFORE you refresh then you simply select the row with an ID of 100 AFTER you refresh.

If you want to not clear the data then you're going to need to determine the last ID in the current data and then only retrieve data with an ID greater than that. This assumes, of course, that you are using an auto-generated ID column. That doesn't take into account any records that may have been updated though; only those that have been added. If you want to track updates too then you'd need to add an extra column containing the last modified date and use that to decide which records to retrieve.
 
That's correct. I can use the Primary Key of the table to determine which row is selected and after reload use a loop to search that row out.

Thanks mate!
 
I'm not 100% sure, but I think doing this might be better, assuming you have done your data setup correctly:

VB.NET:
Me.MyDataSet.MyGridTable.Merge( Me.MyGridTableTableAdapter.GetData() )

The TA will load a whole new datatable from the database, but then Merge will merge the changes rather than clearing them all and refilling them. This MIGHT preserve the row you were on..
 
I'm not 100% sure, but I think doing this might be better, assuming you have done your data setup correctly:

VB.NET:
Me.MyDataSet.MyGridTable.Merge( Me.MyGridTableTableAdapter.GetData() )

The TA will load a whole new datatable from the database, but then Merge will merge the changes rather than clearing them all and refilling them. This MIGHT preserve the row you were on..
If that works then that is seriously sweet.
 
Back
Top