Normally, True DBGrid's default editing behavior is sufficient for most applications. In some cases, however, you may want to customize this behavior. One valuable technique is to use a drop-down list or combo box, or even another True DBGrid control, to allow selection from a list of possible values. This is easy to do with True DBGrid using virtually any Visual Basic or third-party control. The general approach follows, and a working example is given in Tutorial 9 - Attaching an Arbitrary Drop-Down Control to a Grid Cell.
In general, displaying a drop-down list or combo instead of the standard True DBGrid editor involves the following steps:
1. True DBGrid fires the BeforeColEdit event each time the user wants to edit a cell. To override the default editing process, cancel True DBGrid's default editor by setting the Cancel parameter to True. Put code in BeforeColEdit to display the editing control you wish to show instead. Typically, you place the substitute editing control or drop-down on the same form as the grid, but make it invisible until you need it.
2. When BeforeColEdit is triggered, there are five properties and one method that can be used to determine the exact coordinates of the cell that is to be edited. The properties are Left (applies to grid and column), Top (grid and column), CellTop (column only, used with multiple line displays), Width (column only), and RowHeight (grid only). The method is RowTop (grid only). Use these properties and method to position the custom editing control or drop-down relative to a grid cell. For example, place a ListBox control at the right edge of a cell and align its top border with that of the cell using the following code:
Private Sub C1TrueDBGrid1_BeforeColEdit(ByVal sender As Object, ByVal e As C1.Win.C1TrueDBGrid.BeforeColEditEventArgs) Handles C1TrueDBGrid1.BeforeColEdit
Dim r As Rectangle = Me.C1TrueDBGrid1.Splits(0).GetCellBounds(Me.C1TrueDBGrid1.Row, e.ColIndex)
r = Me.C1TrueDBGrid1.RectangleToScreen(r)
r = Me.RectangleToClient(r)
Me.ListBox1.Left = r.Left
Me.ListBox1.Top = r.Bottom
End Sub
· C#
private void c1TrueDBGrid1_BeforeColEdit(object sender, C1.Win.C1TrueDBGrid.BeforeColEditEventArgs e)
{
Rectangle r = this.c1TrueDBGrid1.Splits[0].GetCellBounds(this.c1TrueDBGrid1.Row, e.ColIndex);
r = this.c1TrueDBGrid1.RectangleToScreen(r);
r = this.RectangleToClient(r);
this.ListBox1.Left = r.Left;
this.ListBox1.Top = r.Bottom;
}
· Delphi
procedure C1TrueDBGrid1_BeforeColEdit(sender: object; e: C1.Win.C1TrueDBGrid.BeforeColEditEventArgs);
var
r: Rectangle;
begin
r := Self.C1TrueDBGrid1.Splits[0].GetCellBounds(Self.C1TrueDBGrid1.Row, e.ColIndex);
r := Self.C1TrueDBGrid1.RectangleToScreen(r);
r := Self.RectangleToClient(r);
Self.ListBox1.Left := r.Left;
Self.ListBox1.Top := r.Bottom;
end;
3. Put code in the drop-down or combo which completes the editing process by assigning the selected value to the Text or Value property of the column being edited.
This method does not work, however, when the grid's MarqueeStyle property is set to the value of MarqueeEnum.FloatingEditor. When the floating editor marquee is used, the BeforeColEdit event does not fire until the cell has been changed by the user. However, use the built-in column button feature to activate the drop-down as described in the next section.
For illustrations of other MarqueeStyle settings, see Highlighting the Current Row or Cell. An example of dropping down a Visual Basic ListBox control from a grid cell is given in Tutorial 9 - Attaching an Arbitrary Drop-Down Control to a Grid Cell.
Send comments about this topic to ComponentOne. Copyright © ComponentOne LLC. All rights reserved. |