Skip to main content

How to: Assign editors to individual cells (replace default editors for specific cells)

  • 3 minutes to read

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

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 }
        };
    }
}

The image below shows the result:

Assign Editors to Individual Cells - WinForms TreeList