KB Article T923817
Visible to All Users

Replace Default Cell Editors - WinForms Cheat Sheet

Other DevExpress WinForms Cheat Sheets


DevExpress editor containers (GridControl, TreeList, VGridControl, PivotGridControl etc.) utilize in-place DevExpress Editors to display and edit data source records. These controls automatically choose editors depending on column data type (for instance, if a column displays dates, it will utilize the DateEdit in-place editor).

You can replace a default editor for an entire field or for an individual cell (you can also assign an editor to be used only for editing).

  1. Add required Repository items to the RepositoryItems collection of a data-aware control (see the EditorContainer.RepositoryItems property) or to an external repository (see the EditorContainer.ExternalRepository property).

  2. Use the following API to assign editors to columns and/or cells:

    • the ~ColumnEdit property to assign an editor to an entire field (column/row). You can use it both at runtime and at design time;
    • the ~CustomCellEdit event to assign an editor to a separate cell;
    • the ~CustomCellEditForEditing event to assign an editor to a separate cell for editing.

These editors are also used in the built-in Edit Forms (see Grid Edit Form, TreeList Edit Form).

Do not create new RepositoryItems in the CustomRowCellEditForEditing and CustomRowCellEdit event handlers as this may increase memory usage and decrease performance. Instead, create required RepositoryItems and cache them. Then, extract a required RepositoryItem from the cache when necessary. Refer to the How to assign Different Column Editors for Different Rows example below demonstrating a possible "cache" implementation.

The EditorShowMode property allows you to specify how a cell editor is activated - on the mouse up, on clicking inside a focused cell, etc. You can access this property by the following paths:

Properties and Events You Can Use to Assign a Custom Editor

To a field To a specific cell To a specific cell for editing Additional information
Data Grid GridColumn.ColumnEdit CustomRowCellEdit CustomRowCellEditForEditing Cell Values, Editors, and Validation
Tree List / Gantt Control TreeListColumn.ColumnEdit CustomNodeCellEdit CustomNodeCellEditForEditing Assigning Editors to Individual Cells
Vertical Grid / Property Grid RowProperties.RowEdit CustomRecordCellEdit CustomRecordCellEditForEditing Assigning Editors to Editor Rows
Pivot Grid PivotGridField.FieldEdit CustomCellEdit CustomCellEditForEditing Assigning Editors to Individual Cells

For Property Grid, you can also specify an in-place editor to edit specific data type by adding it to a corresponding collection:


Examples

How to create a Column and Assign an Editor to It

  • Create a RepositoryItem and configure it as your needs dictate.
  • Add it to the GridControl.RepositoryItems collection.
  • Assign this RepositoryItem to the GridColumn.ColumnEdit property of a required column.
Code Example
C#
var col = gridView1.Columns.AddVisible("testField"); RepositoryItemLookUpEdit columnEditor = new RepositoryItemLookUpEdit(); //customizing the column editor //... gridControl1.RepositoryItems.Add(columnEditor); col.ColumnEdit = columnEditor;

How to assign Different Column Editors to Different Rows?

  • Create required RepositoryItems.
  • Add them to the RepositoryItems collection.
  • Assign them in the CustomRowCellEdit event handler.
Code Example
C#
RepositoryItemSpinEdit riSpinEdit = new RepositoryItemSpinEdit(); RepositoryItemCalcEdit riCalcEdit = new RepositoryItemCalcEdit(); RepositoryItemTextEdit riTextEdit = new RepositoryItemTextEdit(); RepositoryItem[] inplaceEditors; ... private void Form1_Load(object sender, EventArgs e) { inplaceEditors = new RepositoryItem[] { riSpinEdit, riTextEdit, riCalcEdit }; gridControl1.RepositoryItems.Add(riSpinEdit); gridControl1.RepositoryItems.Add(riCalcEdit); gridControl1.RepositoryItems.Add(riTextEdit); gridView1.CustomRowCellEdit += GridView1_CustomRowCellEdit; } private void GridView1_CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e) { GridView view = sender as GridView; int editorIndex = (view.GetDataSourceRowIndex(e.RowHandle) + e.Column.VisibleIndex) % 3; e.RepositoryItem = inplaceEditors[editorIndex]; }

How to specify Different In-place Editors to Use in Display and Edit Modes?

  • Create required RepositoryItems.
  • Add them to the RepositoryItems collection
  • Editors that should be used only when users edit data should be assigned in the CustomRowCellEditForEditing event handler.
  • Editors that display data and are not used for editing should be assigned to the ColumnEdit property.
Code Example
C#
RepositoryItem editorForDisplay, editorForEditing; private void Form1_Load(object sender, EventArgs e) { editorForDisplay = new RepositoryItemProgressBar(); editorForEditing = new RepositoryItemSpinEdit(); gridView1.GridControl.RepositoryItems.AddRange( new RepositoryItem[] { editorForDisplay, editorForEditing }); gridView1.Columns["Quantity"].ColumnEdit = editorForDisplay; } private void gridView1_CustomRowCellEditForEditing(object sender, CustomRowCellEditEventArgs e) { if (e.Column.FieldName == "Quantity") e.RepositoryItem = editorForEditing; }

How to use different data sources for different cells in the TokenEdit column in DataGrid?

  • Create a separate RepositoryItemTokenEdit and handle the CustomRowCellEditForEditing event to use it for editing when a cell is activated.
  • Update the data source of the RepositoryItemTokenEdit in that event handler and assign it to the e.RepositoryItem parameter.
Code Example
C#
private void GridView1_CustomRowCellEditForEditing(object sender, CustomRowCellEditEventArgs e) { if (e.Column.FieldName == "TargetFieldName") { edit.Tokens.Clear(); for (int i = 1; i < 6; i++) { string value = "Operator" + i * (e.RowHandle + 1); edit.Tokens.Add(new TokenEditToken(value, value)); } e.RepositoryItem = edit; } }

Please also review the following examples from the Replace Default Cell Editors in Code help article:

  • Progress Bar as a Cell Editor for a numeric column
  • Place buttons inside Grid cells
  • Use different editors for cells of the same column
  • Toggle editor buttons visibility or availability

How to set the max length or assign a mask to a field in a data-aware component or its Edit Form?

  • Replace the default column editor either in the designer or in code using the ColumnEdit property.
  • Specify the editor's Input Mask or the MaxLength property.

Help us improve this article

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.