Back to Devexpress

Assign Editors to Editor Rows

windowsforms-429-controls-and-libraries-vertical-grid-data-editing-and-validation-assigning-editors-to-editor-rows.md

latest14.9 KB
Original Source

Assign Editors to Editor Rows

  • Feb 16, 2023
  • 9 minutes to read

This topic describes how to bind repository items (editors) to editor rows. Multi-editor rows can display data from several bound data fields, so you can bind different editors to a multi-editor row. Read the following topic for detailed information and examples: How to Assign Editors to Multi-Editor Rows topic.

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.

Assign Editors at Design Time

Invoke a row’s smart tag menu and use the RowProperties.RowEdit property’s dropdown to create a new editor or choose an existing editor and assign it to the row.

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

Assign Editors at Runtime

Create a repository item, add it to the Vertical Grid’s RepositoryItems collection, and assign the repository item to the RowProperties.RowEdit property.

csharp
using DevExpress.XtraEditors.Repository;

public Form1() {
    InitializeComponent();
    vGridControl1.DataSource = Order.InitData();
    vGridControl1.ForceInitialize();
    vGridControl1.OptionsView.ShowCaption= true;
    vGridControl1.Caption = "Orders";
    // Creates a 'ToggleSwitch' repository item.
    RepositoryItemToggleSwitch toggleSwitch = new RepositoryItemToggleSwitch();
    // Adds the repository item to the Vertical Grid's RepositoryItems collection.
    vGridControl1.RepositoryItems.Add(toggleSwitch);
    // Assigns the repository item to the 'Delivered' column.
    vGridControl1.Rows["Delivered"].Properties.RowEdit = toggleSwitch;
}

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

Public Sub New()
    InitializeComponent()
    vGridControl1.DataSource = Order.InitData()
    vGridControl1.ForceInitialize()
    vGridControl1.OptionsView.ShowCaption= True
    vGridControl1.Caption = "Orders"
    ' Creates a 'ToggleSwitch' repository item.
    Dim toggleSwitch As New RepositoryItemToggleSwitch()
    ' Adds the repository item to the Vertical Grid's RepositoryItems collection.
    vGridControl1.RepositoryItems.Add(toggleSwitch)
    ' Assigns the repository item to the 'Delivered' column.
    vGridControl1.Rows("Delivered").Properties.RowEdit = 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 Name() As String
    Public Property OrderDate() As Date
    Public Property Price() As Double
    Public Property Discount() 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 {.Name = "Order A", .OrderDate = Date.Today, .Price = 199.99, .Discount = 15.99, .Delivered = True},
            New Order(1) With {.Name = "Order B", .OrderDate = Date.Today, .Price = 219.99, .Discount = 5.99, .Delivered = True},
            New Order(2) With {.Name = "Order C", .OrderDate = Date.Today, .Price = 549.99, .Discount = 44.99, .Delivered = False},
            New Order(3) With {.Name = "Order D", .OrderDate = Date.Today, .Price = 889.99, .Discount = 99.99, .Delivered = False}
        }
    End Function
End Class

Assign Editors to Individual Cells

Handle the VGridControlBase.CustomRecordCellEdit event to specify a specific in-place editor for a certain cell or a group of cells. Repository items should be added to the Vertical Grid’s RepositoryItems collection.

csharp
using DevExpress.XtraEditors;
using DevExpress.XtraVerticalGrid;
using DevExpress.XtraEditors.Repository;

public Form1() {
    InitializeComponent();
    vGridControl1.DataSource = Order.InitData();
    vGridControl1.ForceInitialize();
    vGridControl1.OptionsView.ShowCaption= true;
    vGridControl1.Caption = "Orders";
    // Creates a 'SpinEdit' repository item.
    RepositoryItem spinEditor = new RepositoryItemSpinEdit();
    // Creates a 'CalcEdit' repository item.
    RepositoryItem calcEditor = new RepositoryItemCalcEdit();
    // Adds the repository items to the Vertical Grid's RepositoryItems collection.
    vGridControl1.RepositoryItems.AddRange(new RepositoryItem[] { spinEditor, calcEditor });
    /* Handles the 'CustomRecordCellEdit' event to assign different editors to cells
     * in the 'Price' row.
     */
    vGridControl1.CustomRecordCellEdit += (sender, e) => {
        VGridControl vgrid = sender as VGridControl;
        if(e.Row.Properties.FieldName == "Price" && e.RecordIndex != -1)
            e.RepositoryItem = (double)vgrid.GetCellValue(e.Row, e.RecordIndex) < 100 ? spinEditor : calcEditor;
    };
}

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

Public Sub New()
    InitializeComponent()
    vGridControl1.DataSource = Order.InitData()
    vGridControl1.ForceInitialize()
    vGridControl1.OptionsView.ShowCaption= True
    vGridControl1.Caption = "Orders"
    ' 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 Vertical Grid's RepositoryItems collection.
    vGridControl1.RepositoryItems.AddRange(New RepositoryItem() { spinEditor, calcEditor })
' Handles the 'CustomRecordCellEdit' event to assign different editors to cells
' * in the 'Price' row.
'     
    AddHandler vGridControl1.CustomRecordCellEdit, Sub(sender, e)
        Dim vgrid As VGridControl = TryCast(sender, VGridControl)
        If e.Row.Properties.FieldName = "Price" AndAlso e.RecordIndex <> -1 Then
            e.RepositoryItem = If(CDbl(vgrid.GetCellValue(e.Row, e.RecordIndex)) < 100, 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 Name() As String
    Public Property OrderDate() As Date
    Public Property Price() As Double
    Public Property Discount() 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 {.Name = "Order A", .OrderDate = Date.Today, .Price = 199.99, .Discount = 15.99, .Delivered = True},
            New Order(1) With {.Name = "Order B", .OrderDate = Date.Today, .Price = 219.99, .Discount = 5.99, .Delivered = True},
            New Order(2) With {.Name = "Order C", .OrderDate = Date.Today, .Price = 549.99, .Discount = 44.99, .Delivered = False},
            New Order(3) With {.Name = "Order D", .OrderDate = Date.Today, .Price = 889.99, .Discount = 99.99, .Delivered = False}
        }
    End Function
End Class

Replace Default Editors When Editing Cell Values (Only)

Editors assigned to data cells are used to display and edit their values. Handle the VGridControlBase.CustomRecordCellEditForEditing event to use a different editor only for in-place editing.

csharp
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;

public Form1() {
    InitializeComponent();
    vGridControl1.DataSource = Order.InitData();
    vGridControl1.ForceInitialize();
    vGridControl1.OptionsView.ShowCaption= true;
    vGridControl1.Caption = "Orders";
    // Creates a 'ProgressBar' repository item.
    RepositoryItem progress = new RepositoryItemProgressBar();
    // Creates a 'CalcEdit' repository item.
    RepositoryItem calcEditor = new RepositoryItemCalcEdit();
    // Adds the repository items to the Vertical Grid's RepositoryItems collection.
    vGridControl1.RepositoryItems.AddRange(new RepositoryItem[] { progress, calcEditor });
    // Assigns the 'Progress Bar' repository item to the 'Quantity' row.
    vGridControl1.Rows["Quantity"].Properties.RowEdit = progress;

    vGridControl1.CustomRecordCellEditForEditing += (sender, e) => {
        if(e.Row.Properties.FieldName == "Quantity" && e.RecordIndex != -1)
            e.RepositoryItem = calcEditor;
    };
}

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

Public Sub New()
    InitializeComponent()
    vGridControl1.DataSource = Order.InitData()
    vGridControl1.ForceInitialize()
    vGridControl1.OptionsView.ShowCaption= True
    vGridControl1.Caption = "Orders"
    ' Creates a 'ProgressBar' repository item.
    Dim progress As RepositoryItem = New RepositoryItemProgressBar()
    ' Creates a 'CalcEdit' repository item.
    Dim calcEditor As RepositoryItem = New RepositoryItemCalcEdit()
    ' Adds the repository items to the Vertical Grid's RepositoryItems collection.
    vGridControl1.RepositoryItems.AddRange(New RepositoryItem() { progress, calcEditor })
    ' Assigns the 'Progress Bar' repository item to the 'Quantity' row.
    vGridControl1.Rows("Quantity").Properties.RowEdit = progress

    AddHandler vGridControl1.CustomRecordCellEditForEditing, Sub(sender, e)
        If e.Row.Properties.FieldName = "Quantity" AndAlso e.RecordIndex <> -1 Then
            e.RepositoryItem = 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 Name() As String
    Public Property OrderDate() As Date
    Public Property Price() As Double
    Public Property Quantity() As Integer
    Public Shared Function InitData() As List(Of Order)
        Return New List(Of Order) From {
            New Order(0) With {.Name = "Order A", .OrderDate = Date.Today, .Price = 199.99, .Quantity = 80},
            New Order(1) With {.Name = "Order B", .OrderDate = Date.Today, .Price = 219.99, .Quantity = 45},
            New Order(2) With {.Name = "Order C", .OrderDate = Date.Today, .Price = 549.99, .Quantity = 32},
            New Order(3) With {.Name = "Order D", .OrderDate = Date.Today, .Price = 889.99, .Quantity = 77}
        }
    End Function
End Class

The image below shows the result:

See Also

In-place Editors

Edit Data. Create Cell Editors. Validate User Input