This example demostrates how to update GridControl data in a separate thread. To synchronize access to data, call the BeginDataUpdate and EndDataUpdate methods. These methods allow you to lock updates within the GridControl, process data updates, and then apply all changes to the control.
Create a custom service that calls the GridControl methods. Use the Dispatcher to invoke these methods in the UI thread.
...
public interface IGridUpdateService {
void BeginUpdate();
void EndUpdate();
}
public class GridUpdateService : ServiceBase, IGridUpdateService {
//...
public void BeginUpdate() {
Dispatcher.Invoke(new Action(() => {
if (GridControl != null) {
GridControl.BeginDataUpdate();
}
}));
}
public void EndUpdate() {
//...
}
}
...
Add the service to your View and assosiate this service with the GridControl.
<dxg:GridControl>
<mvvm:Interaction.Behaviors>
<local:GridUpdateService />
</mvvm:Interaction.Behaviors>
</dxg:GridControl>
...
Access the service at the View Model level. If you inherit your View Model class from ViewModelBase, you can access the service as follows:
public class ViewModel : ViewModelBase {
//...
public IGridUpdateService GridUpdateService { get { return GetService<IGridUpdateService>(); } }
void TimerCallback(object state) {
lock(SyncRoot) {
if(GridUpdateService != null) {
GridUpdateService.BeginUpdate();
foreach(DataItem item in Source) {
item.Value = random.Next(100);
}
GridUpdateService.EndUpdate();
}
}
}
}
- GridUpdateService.cs (VB: GridUpdateService.vb)
- MainWindow.xaml (VB: MainWindow.xaml)
- ViewModel.cs (VB: ViewModel.vb)
Refer to the following topics for information on how to access a service in the ViewModel:
- Services in ViewModelBase Descendants
- Services in Generated ViewModels
- Services in Custom ViewModels
(you will be redirected to DevExpress.com to submit your response)