Back to Devexpress

GridViewBase.CellValueChanging Event

wpf-devexpress-dot-xpf-dot-grid-dot-gridviewbase-549403e0.md

latest11.9 KB
Original Source

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.v25.2.dll

NuGet Package : DevExpress.Wpf.Grid.Core

Declaration

csharp
public event CellValueChangedEventHandler CellValueChanging
vb
Public Event CellValueChanging As CellValueChangedEventHandler

Event Data

The CellValueChanging event's data class is CellValueChangedEventArgs. The following properties provide information specific to this event:

PropertyDescription
CellGets a processed cell. Inherited from CellValueEventArgs.
ColumnGets a column that contains the edited cell. Inherited from CellValueEventArgs.
HandledGets 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.
OldValueGets the cell’s previous value.
OriginalSourceGets the original reporting source as determined by pure hit testing, before any possible Source adjustment by a parent class. Inherited from RoutedEventArgs.
RoutedEventGets or sets the RoutedEvent associated with this RoutedEventArgs instance. Inherited from RoutedEventArgs.
RowGets the processed row. Inherited from RowEventArgs.
RowHandleGets the processed row’s handle. Inherited from RowEventArgs.
SourceGets or sets a reference to the object that raised the event. Inherited from RoutedEventArgs.
ValueGets or sets the processed cell’s value. Inherited from CellValueEventArgs.

The event data class exposes the following methods:

MethodDescription
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.

csharp
void OnEditFormCellValueChanging(object sender, CellValueChangedEventArgs e) {
    CellValueChangedInEditFormEventArgs editFormArgs = e as CellValueChangedInEditFormEventArgs;
    // ...
}
vb
Private Sub OnEditFormCellValueChanging(ByVal sender As Object, ByVal e As CellValueChangedEventArgs)
    Dim editFormArgs As CellValueChangedInEditFormEventArgs = TryCast(e, CellValueChangedInEditFormEventArgs)
    ' ...
End Sub

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.

csharp
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;
    }
}
vb
Private Sub OnEditFormCellValueChanging(ByVal sender As Object, ByVal e As CellValueChangedEventArgs)
' ...
    If Equals(e.Cell.[Property], NameOf(DataItem.CanEdit)) Then
        Dim priceData = editFormArgs.CellEditors.FirstOrDefault(Function(x) Equals(x.FieldName, NameOf(DataItem.Price)))
        priceData.[ReadOnly] = Not Boolean.Parse(e.Cell.Value.ToString())
        Return
    End If

    If Equals(e.Cell.[Property], NameOf(DataItem.Price)) Then
        Dim positionValueData = editFormArgs.CellEditors.First(Function(d) Equals(d.FieldName, NameOf(DataItem.PositionValue)))
        Dim amountData = editFormArgs.CellEditors.First(Function(d) Equals(d.FieldName, NameOf(DataItem.Amount)))
        Dim price As Integer = 0
        Call Integer.TryParse(CStr(e.Value), price)
        positionValueData.Value = CInt(amountData.Value) * price
    End If
End Sub

View Example: Data Grid for WPF - How to Process Related Cells in the Edit Form

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.

wpf-grid-sync-isnodeexpanded-with-view-model/CS/DevExpress.Example04/MainWindow.xaml#L43

xml
<dxg:GridControl.View>
    <dxg:TableView CellValueChanging="TableView_CellValueChanging" AllowGrouping="False"/>
</dxg:GridControl.View>

wpf-data-grid-edit-form-related-cells/CS/SynchronizeEditValuesInEditForm_CodeBehind/MainWindow.xaml#L19

xml
<dxg:GridControl.View>
    <dxg:TableView EditFormShowMode="Inline" RowEditStarting="OnRowEditStarting" CellValueChanging="OnEditFormCellValueChanging"/>
</dxg:GridControl.View>

wpf-grid-sync-isnodeexpanded-with-view-model/VB/DevExpress.Example04/obj/Debug/net8.0-windows/MainWindow.g.vb#L118

vb
#ExternalSource("..\..\..\MainWindow.xaml",43)
AddHandler CType(target,DevExpress.Xpf.Grid.TableView).CellValueChanging, New DevExpress.Xpf.Grid.CellValueChangedEventHandler(AddressOf Me.TableView_CellValueChanging)

See Also

CellValueChanged

GridViewBase Class

GridViewBase Members

DevExpress.Xpf.Grid Namespace