baaroz
baaroz

Reputation: 19587

update datagridview when database change

I have 2 computer: server and client,same winform app on both,same database.

I want to be able to update the datagridview on database change

So I made a ticker update every 4 seconds that refresh the datagridview datasource.

Few problems when the datasource is changing

First if a row was choose(dataGridView_RowHeaderMouseClick) inside the datagrid ,it lost focus.

seconed if I scroll down the datagridview,the scroll bar jump to the start.

Any Idea on how to do it right?

Thanks

Baaroz

Upvotes: 0

Views: 1371

Answers (2)

erictrigo
erictrigo

Reputation: 1017

You do not want to refresh the DataSource every 4 seconds without any conditions. As you say, there are several functionality issues that will be affected by this (such as losing focus on a row, that although could be solved by storing the row handle every time you focus into a row, shouldn't be something that you need to do), without mentioning the fact that if the tables you are loading are large there will be a performance issue caused by the constant reload.

You should either trigger a refresh every time the DataSource changes, or what Anthbs says, compare the data with your grid's DataSource and only refreshing if they are different.

Upvotes: 1

Anthbs
Anthbs

Reputation: 184

Heres a couple of idea's to reduce the refresh issues.

  • To reduce the chance of this occuring you could do a comparison of the new data with the data in the grid only refresh if the data has changed.

  • You could store the selected index before you refresh the grid and set it back after you have refreshed.

Upvotes: 0

Related Questions