Skip to content

DevExpress-Examples/wpf-data-grid-update-data-in-a-separate-thread

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

62 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Data Grid for WPF - How to Update Data in a Separate Thread

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();
            }
        }
    }
}

Files to Look At

Documentation

Refer to the following topics for information on how to access a service in the ViewModel:

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)