The following sections describe how to use and create custom grid editors.
The built-in editors provide a lot of flexibility and power, but in some cases you may want to use external controls as specialized editors. For example, you may want to use the C1NumericEdit control that provides a drop-down calculator for entering numbers, or an editor for selecting from multi-column lists, or a specialized control that you wrote to edit your business objects.
Any control that derives from the base Control class can be used as a basic grid editor. Controls that implement the IC1EmbeddedEditor interface can provide better integration with the grid and more advanced features. For details on the IC1EmbeddedEditor interface, see the Editor property.
To use a control as a custom editor, all you have to do is associate an instance of the control with a grid column using the Editor property. You can do this in code using the Editor property. After that, the control will be automatically used by the grid.
For example, to use a C1NumericEdit control as a grid editor, follow these steps:
C# |
Copy Code
|
---|---|
// Set up the custom editor. this.c1NumericEdit1.BorderStyle = BorderStyle.None; this.c1NumericEdit1.Visible = false; |
C# |
Copy Code
|
---|---|
private void Form_Load(object sender, EventArgs e) { // Assign the custom editor to the grid. this.c1TrueDBGrid1.Columns[0].Editor = this.c1NumericEdit1; } |
Run the project and edit some values in the first column. Notice how the grid positions and initializes the C1NumericEdit control so you can edit cell values. When you are done editing a cell, click a different cell or press the TAB key to move to the next one. Notice how the new value is applied to the cell.
Any control that derives from the Control base class can be used as a grid editor. This is possible because the grid knows enough about the base class to access properties such as Text and Bounds, and events such as Leave and TextChanged. In many cases this level of support is adequate.
In some cases, however, you may want to use controls that do not follow the base class that closely. For example, a DateTimePicker control has a Value property that should be used to retrieve the edited value instead of Text. In these cases, you can implement one or more methods in the IC1EmbeddedEditor interface to override the default behavior. For example, all controls in the C1Input library support IC1EmbeddedEditor and therefore integrate closely with C1TrueDBGrid (and also C1FlexGrid).
The IC1EmbeddedEditor interface is fairly simple, and because the grid binds to it using late binding, you do not even have to implement all its members. Only implement the ones that make sense to your editor control.
The interface does provide enough flexibility to allow virtually any control to be used as a grid editor. You can even use UITypeEditor classes as grid editors. To do this, you need a wrapper class that: