Cell Edit

A cell can enter edit mode if the user double-clicked on a cell (by default), or press F2 on the control. A text box will be placed on the control for receipting the user's input.

An input form sample: Styled Spreadsheet

Check Editing Cell

Method IsEditing can be used to check whether a cell is on edit mode.

bool result = sheet.IsEditing;

Method GetEditingCell can be used to get the position of current edit cell:

ReoGridCell cell = sheet.GetEditingCell();

Edit Control

Initiating Cell Editing

To begin editing a specific cell programmatically, the StartEdit method is employed. This method can be invoked with either row and column indices or a CellPosition object:

// Starting edit operation by specifying row and column indices
sheet.StartEdit(2, 3);  // Start editing at the cell located at row 3, column 4

// Starting edit operation using a CellPosition object
sheet.StartEdit(new CellPosition("A1"));  // Start editing at cell A1

It's important to note that if any cell is already in edit mode when a new StartEdit command is issued, the current edit operation will conclude automatically before the new one commences. This ensures a seamless transition between editing different cells without manual intervention to end the ongoing edit session.

Forcefully Concluding Cell Editing

The EndEdit method is designed to forcibly terminate an ongoing edit operation within a cell. It accepts a ReoGridEndEditReason parameter, which dictates the subsequent action following the edit's conclusion. For instance, specifying ReoGridEndEditReason.Cancel instructs the control to discard the modifications made during the edit and revert the cell's content to its previous state.

// Forcefully end the current edit operation with a specific reason

Additionally, the EndEdit method can be invoked with arguments to directly assign a new value to the cell, effectively replacing the user's input. This can be done either with or without specifying an end edit reason:

// End the edit operation and set the cell's data to a specific value
sheet.EndEdit("New Value");

// End the edit operation, set the cell's data, and specify the reason for ending the edit
sheet.EndEdit("New Value", ReoGridEndEditReason.Normal);

These variations of the EndEdit method provide flexibility in handling the termination of cell edits, allowing for both the enforcement of specific data values and the control of edit termination behavior.

Readonly cell

Property Readonly of cell instance can be used to disable the data edit, as well as paste operation.

var cell = sheet.Cells["A1"];
cell.Readonly = true;

Handling Cell Edit Events

Handling cell edit events in ReoGrid allows for dynamic interaction with cell data during the editing process. This capability enables not only the capture of inputted characters but also the modification of the entered data with predefined values. Here’s an enhanced explanation with an example to illustrate this concept.

ReoGrid orchestrates the cell editing process as follows:


Before Editing

When a user initiates editing by pressing F2 or entering characters (including those inputted via an IME tool), ReoGrid suspends other processes and transitions to Edit mode. Prior to displaying the input field, the BeforeCellEdit event is triggered, offering an opportunity to cancel the edit operation through the IsCancelled argument.

Preventing Cell Editing

To inhibit cell editing, such as through user double-clicks or pressing F2, the BeforeCellEdit event can be harnessed. By setting the IsCancelled property to true within this event's handler, the initiation of the edit process can be effectively blocked:

// Subscribe to the BeforeCellEdit event
sheet.BeforeCellEdit += (sender, eventArgs) => {
    // Prevent cell editing by setting IsCancelled to true
    eventArgs.IsCancelled = true;

This approach ensures that when the BeforeCellEdit event is triggered—whether by a double-click or the F2 key—the editing process is immediately halted by the IsCancelled flag, maintaining the cell's current state without allowing user modifications.

During Editing

The CellEditTextChanging event is triggered whenever the text in the cell is modified. It is agnostic of the input method, meaning it responds to direct typing, paste operations, and more. This event is ideal for validating the entire string of text or replacing the text with predefined data.

The CellEditCharInputted event occurs for each character inputted into the cell, including characters from an IME tool. It's particularly useful for intercepting and modifying individual characters as they are entered.

In the WPF edition, while the CellEditCharInputted event can identify inputted characters, it doesn't permit their alteration. To modify text during editing, the CellEditTextChanging event should be used instead.

After Editing

The editing session concludes when the user presses 'Enter' or shifts focus away from the input field, leading to the invocation of the AfterCellEdit and CellDataChanged events. It's important to note that pressing the 'Escape' key cancels the edit, reverting the cell to its pre-edit state. Although AfterCellEdit is still called in this scenario, CellDataChanged is not, as the data remains unchanged.

Example: Transforming Input Data

By subscribing to cell edit events, custom logic can be applied to the editing process, such as validating or transforming user input in real-time.

Consider a scenario where you want to replace specific user input with a predefined string. You might use the CellEditTextChanging event to monitor and modify the input text:

void OnCellEditTextChanging(object sender, CellEditTextChangingEventArgs e)
  // Check if the inputted text matches a certain condition
  if (e.NewText.Equals("specific input"))
      // Replace the inputted text with the specified data
      e.NewText = "predefined data";

In this example, when a user enters "specific input" into any cell, the text is automatically replaced with "predefined data". This demonstrates how event handling can be leveraged to control and customize the cell editing experience in ReoGrid.

To activate these event handlers, you must subscribe to the events on the worksheet or grid control:

reoGridControl.CurrentWorksheet.CellEditTextChanging += OnCellEditTextChanging;
// Similarly, subscribe to CellEditCharInputted as needed

This approach ensures that your custom logic is evaluated during the cell editing process, providing a tailored user experience based on your application's requirements.

AfterCellEdit vs. CellDataChanged

The AfterCellEdit event provides a last opportunity to abort the edit, with the possibility of marking the operation as canceled within AfterCellEditEventArgs. This action restores the cell to its state prior to editing. Conversely, the CellDataChanged event signals any alteration to cell data, not limited to user edits but also including programmatic changes via methods like SetCellData or data pasting. This event represents the final stage for observing and potentially modifying cell data post-edit.

Was the content of the page helpful?