Back to Devexpress

TreeListView.CellValueChanging Event

wpf-devexpress-dot-xpf-dot-grid-dot-treelistview-02ffc69a.md

latest10.0 KB
Original Source

TreeListView.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 TreeListCellValueChangedEventHandler CellValueChanging
vb
Public Event CellValueChanging As TreeListCellValueChangedEventHandler

Event Data

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

PropertyDescription
CellGets a processed cell. Inherited from TreeListCellValueEventArgs.
ColumnGets a column that contains the edited cell. Inherited from TreeListCellValueEventArgs.
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.
NodeGets the processed node. Inherited from TreeListNodeEventArgs.
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 TreeListNodeEventArgs.
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 TreeListCellValueEventArgs.

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 editor 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 Node and Column properties identify a cell whose value is changing. The TreeListCellValueEventArgs.Value property returns a new cell value.

The CellValueChanging event does not occur when you change cell values in code.

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 TreeListCellValueChangedEventArgs to TreeListCellValueChangedInEditFormEventArgs.

cs
void OnEditFormCellValueChanging(object sender, TreeListCellValueChangedEventArgs e) {
    TreeListCellValueChangedInEditFormEventArgs editFormArgs = e as TreeListCellValueChangedInEditFormEventArgs;
    // ...
}
vb
Private Sub OnEditFormCellValueChanging(ByVal sender As Object, ByVal e As TreeListCellValueChangedEventArgs)
    Dim editFormArgs As TreeListCellValueChangedInEditFormEventArgs = TryCast(e, TreeListCellValueChangedInEditFormEventArgs)
    // ...
End Sub

The TreeListCellValueChangedInEditFormEventArgs 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.

cs
void OnEditFormCellValueChanging(object sender, TreeListCellValueChangedEventArgs 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 TreeListCellValueChangedEventArgs)
    // ...

    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

See Also

CellValueChanged

CellValueChangingCommand

TreeListView Class

TreeListView Members

DevExpress.Xpf.Grid Namespace