Skip to main content
A newer version of this page is available. .

Edit Cell Values in UI

  • 4 minutes to read

The GridControl uses in-place data editors to display and edit cell values:

Run Demo: Cell Editors

Availability

Data editing is allowed if the DataViewBase.AllowEditing property is set to true and the DataViewBase.NavigationStyle property is set to GridViewNavigationStyle.Cell.

Individual columns have the ColumnBase.AllowEditing property. The property’s default value is Default - the View controls the column’s behavior. Set this property to true or false to override the default behavior. For example, you can set the column’s ColumnBase.AllowEditing property to false to prevent users from changing its values.

If the column’s ColumnBase.ReadOnly property is set to true:

  • Users cannot modify the column’s cell values (they can invoke cell editors, select, and copy their values).
  • You can use the GridControl.SetCellValue method to change cell values in code.

Show and Hide Editors in UI

Users can switch the GridControl to edit mode in the following ways:

  • Click the required cell
  • Press Enter
  • Press F2
  • Start typing.

This activates the focused cell’s in-place editor to allow an end user to modify the value.

To save the changes and close the editor, users should press Enter or move focus to another cell. Pressing Esc closes the editor and discards the changes.

The GridViewBase.ShowingEditor / TreeListView.ShowingEditor events are raised before the focused cell’s editor is activated. These events allow you to cancel the action. After the editor has been shown, the GridControl raises the GridViewBase.ShownEditor / TreeListView.ShownEditor event.

Show and Hide Editors in Code

To invoke the cell’s editor in code, focus the cell and call the DataViewBase.ShowEditor method. Refer to Focusing for information on how to move cell and row focus.

The GridControl provides two methods to hide the active editor:

Method Description
DataViewBase.CloseEditor Hides the active editor and saves the changes.
DataViewBase.HideEditor Hides the active editor and discards the changes.

After the editor has been closed, the GridControl raises the GridViewBase.HiddenEditor / TreeListView.HiddenEditor event.

Customize Editor Activation and Validation

User can select a cell to initiate edit mode. The edit mode in the GridControl works on a per-row basis:

  • When the user selects another cell in the same row, the GridControl validates and queues the new cell value.
  • When the user selects another row, the GridControl validates the modified row and posts the changes to the data source.

When the View is in edit mode, the DataViewBase.ActiveEditor property returns the edited cell’s editor and the DataViewBase.IsEditing property returns true.

Before the active editor’s new value is posted to a data source, it is validated. If a new value is valid, it is posted to a data source and the editor can be closed. Otherwise, if a new value is invalid, the active editor cannot be closed, and cell focus cannot be moved to another cell until its value is correct. Refer to the Cell Validation topic for more information.

You can use the ProcessEditorActivationAction event to process editor activation. The PostEditor() method validates and queues cell changes. The CommitEditing() method validates and posts changes made within the focused row.

Immediate Posting

After a cell was edited, users should unfocus the cell to post the cell’s value to a data source. You can make the GridControl post values to a data source after an end user changes a cell’s value.

Note

The GridControl does not support immediate posting if you use Cell Templates.

Enable Immediate Posting

Set the DataViewBase.EnableImmediatePosting property to true.

Enable Immediate Posting Based on Logic

Handle the GridViewBase.CellValueChanging event and call the DataViewBase.PostEditor method in the event handler. Use this approach to implement complex logic.

The following code sample posts the Name column’s values:

<dxg:TableView CellValueChanging="CellValueChanging" />
void CellValueChanging(object sender, DevExpress.Xpf.Grid.CellValueChangedEventArgs e) {
    if (e.Column.FieldName == "Name")
        e.Source.PostEditor();
}
See Also