Question Datagridview Refreshes

Joined
Mar 18, 2014
Messages
18
Location
Devon, UK
Programming Experience
3-5
Hi all,

Newbie to the forum, so this is my first post!

I'm relatively new to VB.Net, and have a question regarding the DataGridView control.

I have a Form with a DataGridView, which is bound to an underlying table using a BindingSource etc which is working fine.
The App itself is a multiclient app so many people will be adding data to the same table

If I choose to edit data for a row way down in the table and then save the data I want to reload the datatable and refresh the datagridview with my updates and any updates of the other users.

The refresh is easy enough by calling the .Fill method and the Datagridview.Refresh, but doing this causes the DGV control to be redrawn, and the top row then becomes the active row. Is there any easy way round to re-populate the datatable with the new updates, but to keep the record just saved as the current record highlighted in the DGV control rather than having the row highlighter shoot to the top every time?

Regards

DG
 
Firstly, there's no reason for you to be calling Refresh on the DataGridView. All it does is redraw the control and has nothing whatsoever to do with the underlying data. If you change that data then the grid is going to redraw itself anyway so doing so a second time is pointless.

Secondly, there's no reason for you to be doing anything to update the grid with changes that you've made yourself. If you have the data in memory already and bound to the grid then any changes should be being made to that bound data first. That means that the grid will show the change before it gets saved to the database. You then simply save the changes from the bound data back to the database. You never discard and re-get the data bound to the grid so your issue with the current row changing disappears.

The hard part is handling updates by other users. You have to design your database with this in mind from the start to make it work smoothly. You would do something like have a column to contain the date/time of the last change and a column to indicate whether the record was active or not. You would never delete a record but rather deactivate it. You can then call Fill on a data adapter intermittently and only retrieve records where the last change is later than the last time you retrieved data. That means that you don't have to discard the data you already have so the current row doesn't have to change in your grid. You would also set the Filter on your BindingSource to exclude inactive records.
 
Back
Top