Back to Devexpress

TreeList.CustomNodeCellEdit Event

windowsforms-devexpress-dot-xtratreelist-dot-treelist-a9c8a1c9.md

latest8.4 KB
Original Source

TreeList.CustomNodeCellEdit Event

Enables editors to be assigned to cells on an individual basis. To avoid performance issues and increased memory consumption, assign repository items that already exist in the TreeList.RepositoryItems collection. Do not create new repository items in this handler.

Namespace : DevExpress.XtraTreeList

Assembly : DevExpress.XtraTreeList.v25.2.dll

NuGet Packages : DevExpress.Win.Navigation, DevExpress.Win.TreeList

Declaration

csharp
[DXCategory("Editor")]
public event GetCustomNodeCellEditEventHandler CustomNodeCellEdit
vb
<DXCategory("Editor")>
Public Event CustomNodeCellEdit As GetCustomNodeCellEditEventHandler

Event Data

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

PropertyDescription
ColumnGets a column to which the cell processed by an event belongs. Inherited from CellEventArgs.
NodeGets the current Tree List node. Inherited from NodeEventArgs.
RepositoryItemGets or sets the editor assigned to the processed cell.

Remarks

The CustomNodeCellEdit event fires for each cell within the Tree List. The event parameter’s properties allow the column and node to which the current cell belongs to be identified. The cell’s in-place editor is represented by the GetCustomNodeCellEditEventArgs.RepositoryItem property. Note that repository items must be added to the repository.

The editor’s repository can be accessed via the Tree List’s EditorContainer.RepositoryItems property.

By default, the editor assigned to a cell via the TreeListColumn.ColumnEdit property or the CustomNodeCellEdit event will be also used for editing the cell’s contents. If you want to use a different editor for in-place editing, handle the TreeList.CustomNodeCellEditForEditing event.

See Inplace Editors to learn more.

Special notes:

  • The CustomNodeCellEdit event fires whenever a cell is updated. For that reason, this event fires relatively frequently. Do not update Tree List visual elements or implement complex logic for the event handler as it can severely impact application performance.

  • The CustomNodeCellEdit event is designed to assign ready-to-use editors to individual data cells. Do not modify properties of any existing Repository Items (including “e.RepositoryItem” objects) on this event.

Example

The following examples handles the TreeList.CustomNodeCellEdit event to assign different repository items to cells based on a condition.

csharp
using DevExpress.XtraEditors.Repository;

public Form1() {
    InitializeComponent();
    treeList1.RootValue = 0;
    // Binds the TreeList to a data source.
    treeList1.DataSource = Order.InitData();
    // Forces the TreeList to initialize its settings.
    treeList1.ForceInitialize();
    // Creates a 'SpinEdit' repository item.
    RepositoryItem spinEditor = new RepositoryItemSpinEdit();
    // Creates a 'CalcEdit' repository item.
    RepositoryItem calcEditor = new RepositoryItemCalcEdit();
    // Adds the repository items to the TreeList's RepositoryItems collection.
    treeList1.RepositoryItems.AddRange(new RepositoryItem[] { spinEditor, calcEditor });
    /* Handles the 'CustomNodeCellEdit' event to assign different editors to cells
     * in the 'Discount' column.
     */
    treeList1.CustomNodeCellEdit += (sender, e) => {
        if(e.Column.FieldName == "Discount")
            e.RepositoryItem = e.Node.HasChildren ? spinEditor : calcEditor;
    };
}

public class Order {
    int fid;
    public Order(int id) {
        this.fid = id;
    }
    public int ID { get { return this.fid; } }
    public int ParentID { get; set; }
    public string Name { get; set; }
    public DateTime OrderDate { get; set; }
    public double Price { get; set; }
    public double Discount { get; set; }
    static public List<Order> InitData() {
        return new List<Order> {
            new Order(0){ ParentID = 0, Name = "Order A", OrderDate = DateTime.Today, Price = 199.99, Discount = 15.99 },
            new Order(1){ ParentID = 0, Name = "Order B", OrderDate = DateTime.Today, Price = 219.99, Discount = 5.99 },
            new Order(2){ ParentID = 1, Name = "Order C", OrderDate = DateTime.Today, Price = 549.99, Discount = 44.99 },
            new Order(3){ ParentID = 1, Name = "Order D", OrderDate = DateTime.Today, Price = 889.99, Discount = 99.99 }
        };
    }
}
vb
Imports DevExpress.XtraEditors.Repository

Public Sub New()
    InitializeComponent()
    treeList1.RootValue = 0
    ' Binds the TreeList to a data source.
    treeList1.DataSource = Order.InitData()
    ' Forces the TreeList to initialize its settings.
    treeList1.ForceInitialize()
    ' Creates a 'SpinEdit' repository item.
    Dim spinEditor As RepositoryItem = New RepositoryItemSpinEdit()
    ' Creates a 'CalcEdit' repository item.
    Dim calcEditor As RepositoryItem = New RepositoryItemCalcEdit()
    ' Adds the repository items to the TreeList's RepositoryItems collection.
    treeList1.RepositoryItems.AddRange(New RepositoryItem() { spinEditor, calcEditor })
    ' Handles the 'CustomNodeCellEdit' event to assign different editors to cells
    ' in the 'Discount' column.
    AddHandler treeList1.CustomNodeCellEdit, Sub(sender, e)
        If e.Column.FieldName = "Discount" Then
            e.RepositoryItem = If(e.Node.HasChildren, spinEditor, calcEditor)
        End If
    End Sub
End Sub

Public Class Order
    Private fid As Integer
    Public Sub New(ByVal id As Integer)
        Me.fid = id
    End Sub
    Public ReadOnly Property ID() As Integer
        Get
            Return Me.fid
        End Get
    End Property
    Public Property ParentID() As Integer
    Public Property Name() As String
    Public Property OrderDate() As Date
    Public Property Price() As Double
    Public Property Discount() As Double
    Public Shared Function InitData() As List(Of Order)
        Return New List(Of Order) From {
            New Order(0) With {.ParentID = 0, .Name = "Order A", .OrderDate = Date.Today, .Price = 199.99, .Discount = 15.99},
            New Order(1) With {.ParentID = 0, .Name = "Order B", .OrderDate = Date.Today, .Price = 219.99, .Discount = 5.99},
            New Order(2) With {.ParentID = 1, .Name = "Order C", .OrderDate = Date.Today, .Price = 549.99, .Discount = 44.99},
            New Order(3) With {.ParentID = 1, .Name = "Order D", .OrderDate = Date.Today, .Price = 889.99, .Discount = 99.99}
        }
    End Function
End Class

The image below shows the result:

See Also

ColumnEdit

CustomNodeCellEditForEditing

Inplace Editors

TreeList Class

TreeList Members

DevExpress.XtraTreeList Namespace