views:

512

answers:

5

Hello! I have a datagridview and a combobox which get populated randomly with data. However, the new data is never displayed automatically. Someone mentioned the idea of invalidating the control to force the application to redraw it and thus iterate through the contents and populate the control with the new data.

Does anyone know which is the best method for implementing auto-updating controls in windows forms applications?

help greatly appreciated,

regards.

A: 

I suppose you use databinding ? You use databinding on custom objects ?

in that case, your classes have to implement the INotifyPropertyChanged interface.

Frederik Gheysels
A: 

Usually setting the datasource property of the datagridview is enough to cause the datagridview to invalidate itself and redraw. On occasions I recall that I might have had to set the datagridview.datasource = null first and then set the datasource again to cause an update.

Calanus
A: 

If you use a BindingSource, the BindingSource.ResetBindings method might help you.

Thorsten Dittmar
A: 

Sometimes, invalidating a control doesn't always work. Because the WM_PAINT (the underlying windows message) is one of the lowest priority message in the message queue. Hence Windows ignores paint requests if they are too often. (This is another reason why we see a "White Window" with just the border in applications that stopped responding. To circumvent this, call Application.DoEvents(); That will force the queue to process all the events including your request to repaint it.

Mugunth Kumar
A: 

If you are talking about a control that gets populated with data from a database outside of your application, then there is no way for the control to know that the data has changed unless you re-query the database.

If on the other hand you have an application that is doing the data changes itself, then you can implement the INotifyPropertyChanged interface which means you can have a PropertyChanged event that your control reacts to so it updates when something changes. The following page has an example of implementing this: http://www.codeproject.com/KB/cs/BindBetterINotifyProperty.aspx

The datagridview and combobox are populated with data from an xml document. Is it still possible to implement the INotifyPropertyChanged interface to allow for auto-updating?
Goober
Only if the XML document is saved/generated from within your application. If it comes from an external source then you will have to actively montitor it for changes. If the XML document is a file, then you could use a FileSystemWatcher control to periodically test if it has been modified and then just get your controls to reload it.