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
- Edit Form
- RowEditStarted / NodeEditStarted
- RowEditFinished / NodeEditFinished
- RowEditStartedCommand / NodeEditStartedCommand
- RowEditFinishedCommand / NodeEditFinishedCommand
More Examples
- Data Grid for WPF - How to Process Related Cells in the Edit Form
- Data Grid for WPF - How to Specify Edit Form Settings
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
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);
}
}
}
}
}
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>
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);
}
}
}
}
}
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>