Skip to main content

Custom Cell In-place Editors

  • 4 minutes to read

This topic describes how to assign custom in-place editors to worksheet cells. An in-place editor in the Spreadsheet is activated when a user double-clicks a cell or presses F2 when a cell is selected. Custom cell editors can help you implement multiple usage scenarios such as the ability to create a data entry form within a workbook.

Select a Custom Editor from a Predefined Set

The SpreadsheetControl supports a predefined set of editors you can assign to cells without any configuration. This set includes:

Combo box

Spreadsheet_Example_CustomCellEditors_ComboBox

Date picker

Spreadsheet_Example_CustomCellEditors_DatePicker

Check box

Spreadsheet_Example_CustomCellEditors_CheckBox

To assign one of these editors to a cell or cell range, use the CustomCellInplaceEditorCollection.Add method of the worksheet’s Worksheet.CustomCellInplaceEditors collection. A CustomCellInplaceEditorType enumeration value specifies the created editor’s type.

If you use a combo box as a cell’s in-place editor (CustomCellInplaceEditorType.ComboBox), the value parameter of the CustomCellInplaceEditorCollection.Add method allows you to supply items for the editor’s drop-down list. You can use a string of comma-separated values or retrieve items from a cell range via the ValueObject.FromRange method. Value types other than a string or cell range are not permitted.

The example below demonstrates how to assign the above-mentioned editors to table cells.

Note

A complete sample project is available in the DevExpress-Examples/how-to-assign-custom-in-place-editors-to-worksheet-cells-t385401 repository on GitHub.

// Use a date editor as the in-place editor for cells located in the "Order Date" column.
CellRange dateEditRange = worksheet["Table[Order Date]"];
worksheet.CustomCellInplaceEditors.Add(dateEditRange, CustomCellInplaceEditorType.DateEdit);

// Use a combo box as the in-place editor for cells located in the "Category" column.
// The editor's items are obtained from a cell range in the current worksheet.
CellRange comboBoxRange = worksheet["Table[Category]"];
worksheet.CustomCellInplaceEditors.Add(comboBoxRange, CustomCellInplaceEditorType.ComboBox, ValueObject.FromRange(worksheet["J3:J9"]));

// Use a check editor as the in-place editor for cells located in the "Discount" column.
CellRange checkBoxRange = worksheet["Table[Discount]"];
worksheet.CustomCellInplaceEditors.Add(checkBoxRange, CustomCellInplaceEditorType.CheckBox);

Use the CustomCellEdit event to Assign Your Own Editor to a Cell

If custom editors from the predefined set do not comply with your requirements, handle the SpreadsheetControl.CustomCellEdit event to assign your own editor to a cell. This event fires each time an end-user starts to edit a cell and allows you to replace the built-in cell editor with a custom one. The event’s Cell, RowIndex and ColumnIndex parameters help you identify the currently edited cell. To supply a custom editor to the cell, create a RepositoryItem descendant corresponding to the editor you want to use and assign it to the event’s SpreadsheetCustomCellEditEventArgs.RepositoryItem parameter. Refer to the Editors and Simple Controls topic for additional information on this mechanism. The Included Controls and Components document lists available editors and their corresponding repository items.

The code snippet below shows how to assign a spin editor to the sixth column’s cells.

Spreadsheet_Example_CustomCellEditors_SpinEdit

spreadsheetControl.CustomCellEdit += spreadsheetControl_CustomCellEdit;
// ...

// Create a repository item corresponding to a SpinEdit control
RepositoryItemSpinEdit repository = new RepositoryItemSpinEdit();
private void spreadsheetControl_CustomCellEdit(object sender, DevExpress.XtraSpreadsheet.SpreadsheetCustomCellEditEventArgs e)
{
    if (e.SheetName == "Sales report" && e.ColumnIndex == 5 && e.RowIndex > 1)
    {
        //Specify the repository item settings.
        repository.AutoHeight = false;
        repository.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;

        repository.MinValue = 1;
        repository.MaxValue = 1000;
        repository.IsFloatValue = false;
        // Assign the spin editor to a cell.
        e.RepositoryItem = repository;
    }
}
See Also