Showing and Hiding Editors

  • 6 minutes to read

This topic contains information on how and when cell editors can be invoked and hidden programmatically and how to access the active editor and its value. Programmatically hiding and showing an editor is useful if you want to facilitate data entry or searching. For example, you can display an editor for a specific cell when the application starts, so end-users can immediately start typing. Or you can navigate to a specific cell and activate its editor at runtime to help users navigate through the desired data cells only.

For information on how end-users can invoke editors for data cells, please refer to the Editing Data and Navigating Through Cells topics.

The information in this topic is divided into three parts:

When Data Cells Can Be Edited by End-Users

A cell editor can be displayed if the following conditions are met:

Let's consider the last condition in detail. The VGridControlBase.ShowingEditor event fires each time a cell editor is about to be displayed. This implies that the cell for which it is displayed is currently focused. Thus, you can use the VGridControlBase.FocusedRow and VGridControlBase.FocusedRecord properties to identify it. When the cell has been determined, you can permit or prohibit its editor from displaying using the event's Cancel parameter. If it is set to true, the editor will not be displayed.

The sample code below handles the VGridControlBase.ShowingEditor event to prohibit the modification of records whose Trademark field value is 'BMW'. (The VGridControlBase.GetCellValue method is used to obtain the Trademark row value within the current record.)


private void vGridControl1_ShowingEditor(object sender, System.ComponentModel.CancelEventArgs e) {
   VGridControl vGrid = sender as VGridControl;
   string cellValue = vGrid.GetCellValue(vGrid.Rows["rowTrademark"], vGrid.CurrentRecord).ToString();
   if (cellValue == "BMW")
      e.Cancel = true;
}

You can use the VGridControlBase.CanShowEditor property of the grid control to determine whether the focused cell can be edited. This property returns true if all the conditions listed are satisfied.

Showing and Hiding Editors

The focused cell's editor can be invoked by calling the VGridControlBase.ShowEditor method. Thus, if you want to edit a specific cell, you must first focus it by assigning proper values to the VGridControlBase.FocusedRow and VGridControlBase.FocusedRecord properties.

If you want to close the editor programmatically, you can choose one of the two available methods:

Note that you can also use the VGridControlBase.PostEditor method to save changes without closing the editor.

The vertical grid control also enables you to perform specific actions each time an editor is activated or closed either by end-users or programmatically. This can be done by handling the VGridControlBase.ShownEditor and VGridControlBase.HiddenEditor events respectively. The first one can be used to facilitate working with the editor itself. For instance, when using a MemoExEdit editor, you can open its popup window when it is activated (see the example in the following section). The VGridControlBase.HiddenEditor event could be used, for instance, if you want to navigate to the desired cell and activate its editor. The sample code below handles this event to navigate to the next record and activate the editor for the cell residing in the focused row.


private void vGridControl1_HiddenEditor(object sender, System.EventArgs e) {
   VGridControl vGrid = sender as VGridControl;
   if (vGrid.CurrentRecord == vGrid.RecordCount - 1) 
      vGrid.CurrentRecord = 0;
   else 
      vGrid.CurrentRecord++;
   vGrid.ShowEditor();
}

Accessing the Active Editor

When a cell editor is active, you can access it using the control's VGridControlBase.ActiveEditor property. This property returns the BaseEdit object (the common ancestor for all editors in the DevExpress Editors Library). Thus, you need to perform casting to the desired type to use editor-specific properties. Note that if no editor is currently active, the property returns null (Nothing in Visual Basic).

Together with the active editor, you can obtain and modify the cell value currently being edited. This is performed using the VGridControlBase.EditingValue property. This property also returns null (Nothing in Visual Basic) if no editing is occurring at the moment.

The two properties mentioned enable you to customize the editor and the edited value as your needs dictate. For instance, the following sample code handles the VGridControlBase.ShownEditor event to open the popup window of the MemoExEdit editor and write the current date to the edited text. These operations are performed if the editor is opened for the 'Report' row (the MemoExEdit editor must be bound to it).


private void vGridControl1_ShownEditor(object sender, System.EventArgs e) {
   VGridControl vGrid = sender as VGridControl;
   // obtaining the focused row
   if (!(vGrid.FocusedRow is EditorRow)) return;
   EditorRow currentRow = vGrid.FocusedRow as EditorRow;
   if (currentRow.Properties.Caption == "Report") {
      // adding the current date
      string currDate = DateTime.Today.ToShortDateString();
      vGrid.EditingValue = currDate;
      // obtaining the active editor
      MemoExEdit editor = vGrid.ActiveEditor as MemoExEdit;
      // invoking the editor's popup window
      editor.ShowPopup();
   }
}
See Also