Example T1042686
Visible to All Users

Data Grid for WPF - How to Pause Data Updates in the Edit Form

This example illustartates how to pause data updates when a user edits a row in the Edit Form. To do that, handle the RowEditStarted and RowEditFinished events. Alternatively, you can create commands in a View Model and bind them to the RowEditStartedCommand and RowEditFinishedCommand properties.

In this example, the GridControl updates data on a timer. The UpdateRows() function updates data depending on the updatesLocker value. When a user starts to edit a row, the RowEditStarted event occurs. The event handler sets the updatesLocker value to true to lock data updates. When the user finished editing the row, RowEditFinished occurs and the event handler unlocks updates.

C#
bool updatesLocker; // ... private void OnRowEditStarted(object sender, RowEditStartedEventArgs e) => Volatile.Write(ref updatesLocker, true); private void OnRowEditFinished(object sender, RowEditFinishedEventArgs e) => Volatile.Write(ref updatesLocker, false); void UpdateRows(object parameter) { if(!Volatile.Read(ref updatesLocker)) { var row = data[random.Next(0, data.Count)]; if(row.ShouldUpdate) { row.Value = random.Next(1, 100); } } }

In the TreeListView, use the following events and properties:

Files to Look At

Code-Behind

MVVM Pattern

Documentation

More Examples

Does this example address your development requirements/objectives?

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

Example Code

LockOnRowEdit_CodeBehind/MainWindow.xaml.cs(vb)
C#
using DevExpress.Mvvm; using DevExpress.Xpf.Grid; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Windows; namespace LockOnRowEdit_CodeBehind { public class DataItem : BindableBase { public int Id { get => GetValue<int>(); set => SetValue(value); } public string Name { get => GetValue<string>(); set => SetValue(value); } public int Value { get => GetValue<int>(); set => SetValue(value); } public bool ShouldUpdate { get => GetValue<bool>(); set => SetValue(value); } public DataItem(Random random, int id) { Id = id; Name = $"Item {id}"; Value = random.Next(1, 100); ShouldUpdate = true; } } public partial class MainWindow : Window { List<DataItem> data; Timer updateTimer; bool updatesLocker; Random random; public MainWindow() { InitializeComponent(); random = new Random(); grid.ItemsSource = data = new List<DataItem>(Enumerable.Range(0, 20).Select(i => new DataItem(random, i))); updateTimer = new Timer(UpdateRows, null, 0, 1); } private void OnRowEditStarted(object sender, RowEditStartedEventArgs e) => Volatile.Write(ref updatesLocker, true); private void OnRowEditFinished(object sender, RowEditFinishedEventArgs e) => Volatile.Write(ref updatesLocker, false); void UpdateRows(object parameter) { if(!Volatile.Read(ref updatesLocker)) { var row = data[random.Next(0, data.Count)]; if(row.ShouldUpdate) { row.Value = random.Next(1, 100); } } } } }
LockOnRowEdit_CodeBehind/MainWindow.xaml
XAML
<Window x:Class="LockOnRowEdit_CodeBehind.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:LockOnRowEdit_CodeBehind" xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <dxg:GridControl x:Name="grid" AutoGenerateColumns="AddNew"> <dxg:GridControl.View> <dxg:TableView x:Name="view" EditFormShowMode="Inline" RowEditStarted="OnRowEditStarted" RowEditFinished="OnRowEditFinished"/> </dxg:GridControl.View> </dxg:GridControl> </Grid> </Window>
LockOnRowEdit_MVVM/MainViewModel.cs(vb)
C#
using DevExpress.Mvvm; using DevExpress.Mvvm.DataAnnotations; using DevExpress.Mvvm.Xpf; using System; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Threading; using System.Windows.Threading; namespace LockOnRowEdit_MVVM { public class DataItem : BindableBase { public int Id { get => GetValue<int>(); set => SetValue(value); } public string Name { get => GetValue<string>(); set => SetValue(value);} public int Value { get => GetValue<int>(); set => SetValue(value); } public bool ShouldUpdate { get => GetValue<bool>(); set => SetValue(value); } public DataItem(Random random, int id) { Id = id; Name = $"Item {id}"; Value = random.Next(1, 100); ShouldUpdate = true; } } public class MainViewModel : ViewModelBase { public ObservableCollection<DataItem> Data { get => GetValue<ObservableCollection<DataItem>>(); set => SetValue(value); } Random random; Timer updateTimer; bool updatesLocker; public MainViewModel() { random = new Random(); Data = new ObservableCollection<DataItem>(Enumerable.Range(0, 20).Select(i => new DataItem(random, i))); updateTimer = new Timer(UpdateRows, null, 0, 1); } [Command] public void LockUpdates(RowEditStartedArgs args) => Volatile.Write(ref updatesLocker, true); [Command] public void UnlockUpdates(RowEditFinishedArgs args) => Volatile.Write(ref updatesLocker, false); void UpdateRows(object parameter) { if(!Volatile.Read(ref updatesLocker)) { var row = Data[random.Next(0, Data.Count)]; if(row.ShouldUpdate) { row.Value = random.Next(1, 100); } } } } }
LockOnRowEdit_MVVM/MainWindow.xaml
XAML
<Window x:Class="LockOnRowEdit_MVVM.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:LockOnRowEdit_MVVM" xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Window.DataContext> <local:MainViewModel/> </Window.DataContext> <Grid> <dxg:GridControl AutoGenerateColumns="AddNew" ItemsSource="{Binding Data}"> <dxg:GridControl.View> <dxg:TableView EditFormShowMode="Inline" RowEditStartedCommand="{Binding LockUpdatesCommand}" RowEditFinishedCommand="{Binding UnlockUpdatesCommand}"/> </dxg:GridControl.View> </dxg:GridControl> </Grid> </Window>

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.