Back to Devexpress

Assign Editors to Columns

windowsforms-5632-controls-and-libraries-tree-list-feature-center-data-editing-assigning-editors-to-columns.md

latest7.4 KB
Original Source

Assign Editors to Columns

  • Feb 16, 2023
  • 4 minutes to read

This topic explains how to assign DevExpress Editors to TreeList columns.

Watch Video: How To Assign Editors To TreeList Columns in Code

Note

DevExpress WinForms Data Grid, TreeList, Pivot Grid, and Vertical Grid use a similar approach and API to use custom editors instead of default cell editors. Read the following topic to learn more: Cell Values, Editors, and Validation - WinForms Data Grid.

Default Cell Editors

TreeList columns use data editors to display and edit data. Columns automatically create editors depending on the type of data they display. For example, a column that displays date-time values uses the DateEdit.

Data TypeDefault Editor and Repository Item
StringTextEdit, RepositoryItemTextEdit
NumericSpinEdit, RepositoryItemSpinEdit
BooleanCheckEdit, RepositoryItemCheckEdit
Date-TimeDateEdit, RepositoryItemDateEdit
DateTimeOffsetDateTimeOffsetEdit, RepositoryItemDateTimeOffsetEdit
TimeSpanTimeEdit, RepositoryItemTimeEdit
EnumerationComboBoxEdit, RepositoryItemComboBox
Picture/ImagePictureEdit, RepositoryItemPictureEdit

Assign Cell Editors to Columns at Design Time

Invoke a column’s smart tag menu and use the TreeListColumn.ColumnEdit property’s dropdown to create a new editor or choose an existing editor.

You can also run the TreeList Designer and open its In-place Editor Repository page to access all in-place editors. You can add, customize, and remove repository items.

Create Cell Editors and Assign Them to Columns in Code

Create a repository item, add it to the TreeList’s RepositoryItems collection, and assign the repository item to the TreeListColumn.ColumnEdit property.

csharp
using DevExpress.XtraEditors;
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 'ToggleSwitch' repository item.
    RepositoryItemToggleSwitch toggleSwitch = new RepositoryItemToggleSwitch();
    // Adds the repository item to the TreeList's RepositoryItems collection.
    treeList1.RepositoryItems.Add(toggleSwitch);
    // Assigns the repository item to the 'Delivered' column.
    treeList1.Columns["Delivered"].ColumnEdit = toggleSwitch;
}

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 bool Delivered { 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, Delivered = false },
            new Order(1){ ParentID = 0, Name = "Order B", OrderDate = DateTime.Today, Price = 219.99, Delivered = false },
            new Order(2){ ParentID = 1, Name = "Order C", OrderDate = DateTime.Today, Price = 549.99, Delivered = true },
            new Order(3){ ParentID = 1, Name = "Order D", OrderDate = DateTime.Today, Price = 889.99, Delivered = false }
        };
    }
}
vb
Imports DevExpress.XtraEditors
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 'ToggleSwitch' repository item.
    Dim toggleSwitch As New RepositoryItemToggleSwitch()
    ' Adds the repository item to the TreeList's RepositoryItems collection.
    treeList1.RepositoryItems.Add(toggleSwitch)
    ' Assigns the repository item to the 'Delivered' column.
    treeList1.Columns("Delivered").ColumnEdit = toggleSwitch
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 Delivered() As Boolean
    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, .Delivered = False},
            New Order(1) With {.ParentID = 0, .Name = "Order B", .OrderDate = Date.Today, .Price = 219.99, .Delivered = False},
            New Order(2) With {.ParentID = 1, .Name = "Order C", .OrderDate = Date.Today, .Price = 549.99, .Delivered = True},
            New Order(3) With {.ParentID = 1, .Name = "Order D", .OrderDate = Date.Today, .Price = 889.99, .Delivered = False}
        }
    End Function
End Class

The image below shows the result:

See Also

Assign Editors to Individual Cells

Inplace Editors