windowsforms-465-controls-and-libraries-vertical-grid-data-editing-and-validation-showing-and-hiding-editors.md
Users can edit data in the focused cell. To activate the focused cell’s editor, users can press Enter, F2, or click the cell.
An editor can be activated when the left mouse button is pressed or released, and may depend on whether the cell is already focused.
When the left mouse button is pressed in a cell for the first time, the cell gets focus. Then the mouse button is released. After the cell is focused, a user can press and release the button again. You can activate a cell editor at each of these four stages depending on usability requirements.
For example, if a control should support drag-and-drop operations, activate an editor when the mouse button is released, not when it is pressed. On the other hand, if an editor’s drop-down window should be opened at the same time a cell is focused, activate the editor when the mouse button is pressed, not when it is released.
Use the EditorShowMode option to specify how a cell editor is activated with the mouse:
MouseDown — a cell editor is activated when the left mouse button is pressed regardless of whether the cell is focused.
MouseUp — a cell editor is activated when the left mouse button is pressed and released in the same cell regardless of whether the cell is focused.
MouseDownFocused — a cell editor is activated when the left mouse button is pressed in a focused cell.
Click — a cell editor is activated when the left mouse button is pressed and released in a focused cell.
The Default value is equivalent to the MouseDown value except if users can select multiple cells. That is, the MultiSelectMode option is set to CellSelect. In this case, the Default value is equivalent to the Click value.
In MouseDown and MouseDownFocused modes, the editor is activated when the mouse button is pressed and this mouse event is passed to the activated editor. For example, in an ImageComboBoxEdit, this causes the drop-down window to open. On the other hand, the user cannot drag the row.
In MouseUp and Click modes, the editor is not activated when the mouse button is pressed. This results in this mouse event not being passed to the editor. For example, in an ImageComboBoxEdit, this causes the drop-down window to not open. The user should click the editor one more time to open the drop-down window. On the other hand, the user can drag the row.
Note
If the Ctrl, Alt, or Shift key is pressed, the clicked cell is focused, but its editor is not activated.
Use the following properties to specify whether users can edit cells:
You can use the control’s CanShowEditor property to determine whether the focused cell can be edited.
The code below handles the ShowingEditor event to prohibit modification of records whose Trademark field value is set to ‘BMW’. The GetCellValue method returns the Trademark row’s value in 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;
}
Private Sub VGridControl1_ShowingEditor(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles VGridControl1.ShowingEditor
Dim VGrid As VGridControl = sender
Dim CellValue As String = VGrid.GetCellValue(VGrid.Rows("rowTrademark"), VGrid.CurrentRecord)
If CellValue = "BMW" Then
e.Cancel = True
End If
End Sub
Use the following properties to determine the focused cell:
You can also use the editor’s ContainsFocus property to check if the editor is focused. For an editor that can display a drop-down window (for example, a date editor), you can use the EditorContainsFocus property, which returns whether the editor or its drop-down window is focused.
Use the following methods to activate and deactivate the focused cell’s editor:
After an editor is activated, you can use the control’s ActiveEditor property to access it. If there is no active editor in the control, this property returns null ( Nothing in VB). To obtain or set the active editor’s value, use the EditingValue property.
The control raises the following events when a user activates an editor:
The sample code below handles the HiddenEditor event to navigate to the next record and activate the cell editor 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();
}
Private Sub VGridControl1_HiddenEditor(ByVal sender As Object, ByVal e As System.EventArgs) Handles VGridControl1.HiddenEditor
Dim VGrid As VGridControl = sender
If VGrid.CurrentRecord = VGrid.RecordCount - 1 Then
VGrid.CurrentRecord = 0
Else
VGrid.CurrentRecord += 1
End If
VGrid.ShowEditor()
End Sub
The code below handles the ShownEditor event to open a MemoExEdit editor’s drop-down window in the Report row and assign the current date to the cell.
private void vGridControl1_ShownEditor(object sender, System.EventArgs e) {
VGridControl vGrid = sender as VGridControl;
// Obtain the focused row.
if (!(vGrid.FocusedRow is EditorRow)) return;
EditorRow currentRow = vGrid.FocusedRow as EditorRow;
if (currentRow.Properties.Caption == "Report") {
// The current date.
string currDate = DateTime.Today.ToShortDateString();
vGrid.EditingValue = currDate;
// Obtain the active editor.
MemoExEdit editor = vGrid.ActiveEditor as MemoExEdit;
// Invoke the editor's drop-down window.
editor.ShowPopup();
}
}
Private Sub VGridControl1_ShownEditor(ByVal sender As Object, ByVal e As System.EventArgs) Handles VGridControl1.ShownEditor
Dim VGrid As VGridControl = sender
' Obtain the focused row.
If Not TypeOf VGrid.FocusedRow Is EditorRow Then Return
Dim CurrentRow As EditorRow = VGrid.FocusedRow
If CurrentRow.Properties.Caption = "Report" Then
' The current date
Dim CurrDate As String = DateTime.Today.ToShortDateString()
VGrid.EditingValue = CurrDate
' Obtain the active editor.
Dim Editor As MemoExEdit = VGrid.ActiveEditor
' Invoke the editor's drop-down window.
Editor.ShowPopup()
End If
End Sub
See Also