Description:
When I use a standalone editor on a form, I can use methods and properties specific to this editor. However, when a RepositoryItem is used, it doesn't have the necessary methods. So how, for example, can I call the SelectAll method or check the EditValue property when use the RepositoryItem?
Answer:
A repository item is a RepositoryItem class descendant that stores properties and events related to a specific editor. It has all the information required for creating a fully functional editor. I.e. the RepositoryItem is not an editor, it's just a set of properties. Here is a detailed description on how our editors work:
Actual editor classes are BaseEdit descendants. These classes represent stand-alone controls that you can place onto a form. These are also controls that are created by containers when an editor is activated (when editing a cell value, for instance). Each editor control has its associated repository item. Repository items are objects that store editor settings and event handlers. You can access such objects via the editors' Properties property.
For example, if you assigned a repository item to the GridColumn's ColumnEdit property, this repository item specifies the properties for all a column's inplace editors. These editors are not exist until you activate them. When you click within a cell, a corresponding editor is created using the RepositoryItem's CreateEditor method. The properties from the RepositoryItem are assigned to the editor's Properties.
Since one repository item is used to create inplace editors for all cells, it can't provide methods like SelectAll because it's not quite clear for which cell this method should be used.
The solution to this problem is pretty simple: you need to use the editor's methods. You can access a necessary editor in several ways:
1) When a target editor is already activated (required cell is opened for editing)
a) you can access it using the parent container's ActiveEditor property (It can be applied for the following containers: BarManager, GridControl's Views, PivotGridControl, TreeList, VGridControl, PropertyGridControl)
b) You can access the inner TextBoxMaskBox using the form's ActiveControl property (when the editor has focus). Then, to get the editor, read the mask box's Parent property.
2) When a target editor is not active, you can activate it using the container's ShowEditor method.
For example, to select the text in a specific cell of the GridView, you need to use the following code:
C#void SelectAllInEditor(GridView view, int colIndex, int rowIndex) {
view.FocusedColumn = view.VisibleColumns[colIndex];
view.FocusedRowHandle = rowIndex;
view.ShowEditor();
if (view.ActiveEditor != null)
view.ActiveEditor.SelectAll();
}
Visual BasicPrivate Sub SelectAllInEditor(ByVal view As GridView, ByVal colIndex As Integer, ByVal rowIndex As Integer)
view.FocusedColumn = view.VisibleColumns(colIndex)
view.FocusedRowHandle = rowIndex
view.ShowEditor()
If view.ActiveEditor IsNot Nothing Then
view.ActiveEditor.SelectAll()
End If
End Sub
So, if you want to obtain an active editor's value until this value is not posted to the underlying datasource, use the following code:
C#obj value = view.ActiveEditor.EditValue;
Visual BasicDim value As obj = view.ActiveEditor.EditValue
- Let's consider another scenario:
For example, you need to subscribe to an event that doesn't have the RepositoryItem, while a corresponding editor has it.
To accomplish this task, you need to catch the moment, when this editor is created and subscribe to a required event programmatically. For example, you can handle the Container's ShownEditor event, access its ActiveEditor, and subscribe to its events as your needs dictate.
Please refer to the Editors Class Structure topic, for additional information on how our editors work.
See Also:
A1402: Can SelectedIndex be set for RepositoryItemComboBox?
K18473: How to change a RepositoryItem value?
If anyone is reading this please also check following ticket:
https://www.devexpress.com/Support/Center/Question/Details/T194782/problem-focusing-cell-and-showeditor-in-a-grid
Wrapping the code into a methodinvoke saves a lot of hours…
gridView1.GridControl.BeginInvoke(new MethodInvoker(() => { gridView1.FocusedColumn = gridView1.Columns["Info"]; gridView1.ShowEditor(); }));
Cheers!