GridViewBase.CellValueChanging Event
Occurs when a user edits a cell value (for example, types or deletes a character, chooses a value from the drop-down list).
Namespace: DevExpress.Xpf.Grid
Assembly: DevExpress.Xpf.Grid.v24.2.dll
NuGet Package: DevExpress.Wpf.Grid.Core
Declaration
Event Data
The CellValueChanging event's data class is CellValueChangedEventArgs. The following properties provide information specific to this event:
Property | Description |
---|---|
Cell | Gets a processed cell. Inherited from CellValueEventArgs. |
Column | Gets a column that contains the edited cell. Inherited from CellValueEventArgs. |
Handled | Gets or sets a value that indicates the present state of the event handling for a routed event as it travels the route. Inherited from RoutedEventArgs. |
OldValue | Gets the cell’s previous value. |
OriginalSource | Gets the original reporting source as determined by pure hit testing, before any possible Source adjustment by a parent class. Inherited from RoutedEventArgs. |
RoutedEvent | Gets or sets the RoutedEvent associated with this RoutedEventArgs instance. Inherited from RoutedEventArgs. |
Row | Gets the processed row. Inherited from RowEventArgs. |
RowHandle | Gets the processed row’s handle. Inherited from RowEventArgs. |
Source | Gets or sets a reference to the object that raised the event. Inherited from RoutedEventArgs. |
Value | Gets or sets the processed cell’s value. Inherited from CellValueEventArgs. |
The event data class exposes the following methods:
Method | Description |
---|---|
InvokeEventHandler(Delegate, Object) | When overridden in a derived class, provides a way to invoke event handlers in a type-specific way, which can increase efficiency over the base implementation. Inherited from RoutedEventArgs. |
OnSetSource(Object) | When overridden in a derived class, provides a notification callback entry point whenever the value of the Source property of an instance changes. Inherited from RoutedEventArgs. |
Remarks
Handle this event to instantly respond to user actions within an in-place or Edit Form editor. If you want to maintain a clean MVVM pattern and process cell value edits in a View Model, create a command and bind it to the CellValueChangingCommand property.
The RowHandle and Column properties identify a cell whose value is changing. The Value property returns a new cell value.
The CellValueChanging event does not occur when you change cell values in code.
Note
When you use a custom CellTemplate, the GridControl raises the CellValueChanging
event only if the template contains a BaseEdit class descendant declared as demonstrated in the following topic: Custom In-Place Cell Editors.
Refer to the following topic for more information: Obtain and Set Cell Values.
Edit Form
If you process data edit operations in the Edit Form, you need to cast CellValueChangedEventArgs to CellValueChangedInEditFormEventArgs.
void OnEditFormCellValueChanging(object sender, CellValueChangedEventArgs e) {
CellValueChangedInEditFormEventArgs editFormArgs = e as CellValueChangedInEditFormEventArgs;
// ...
}
The CellValueChangedInEditFormEventArgs class contains the CellEditors property that returns an array of CellEditorData objects. Each object allows you to specify editor’s settings. When users edit a row, you may want to initialize values or make certain editors read-only. To do that, specify the corresponding CellEditorData.Value property or set the CellEditorData.ReadOnly property to true.
The following code sample disables the Price editor depending on the CanEdit value and assigns the result of Price and Amount multiplication to the PositionValue editor.
void OnEditFormCellValueChanging(object sender, CellValueChangedEventArgs e) {
// ...
if(e.Cell.Property == nameof(DataItem.CanEdit)) {
var priceData = editFormArgs.CellEditors.FirstOrDefault(x => x.FieldName == nameof(DataItem.Price));
priceData.ReadOnly = !bool.Parse(e.Cell.Value.ToString());
return;
}
if(e.Cell.Property == nameof(DataItem.Price)) {
var positionValueData = editFormArgs.CellEditors.First(d => d.FieldName == nameof(DataItem.PositionValue));
var amountData = editFormArgs.CellEditors.First(d => d.FieldName == nameof(DataItem.Amount));
int price = 0;
int.TryParse((string)e.Value, out price);
positionValueData.Value = (int)amountData.Value * price;
}
}
Related GitHub Examples
The following code snippets (auto-collected from DevExpress Examples) contain references to the CellValueChanging event.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.