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).
-
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).
-
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.
- the
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:
- BarEditItem.EditorShowMode
- ColumnView.OptionsBehavior.EditorShowMode
- PivotGrid.OptionsBehavior.EditorShowMode
- TreeList.OptionsBehavior.EditorShowMode
Properties and Events You Can Use to Assign a Custom Editor
For Property Grid, you can also specify an in-place editor to edit specific data type by adding it to a corresponding collection:
- PropertyGridControl.DefaultEditors - for regular properties
- PropertyGridControl.DefaultCollectionEditors - for collection properties
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.
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.
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.
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.
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.