ComponentOne True DBGrid for WinForms
Cell Editing Techniques / Drop-Down Controls / Using an Arbitrary Drop-Down Control
In This Topic
    Using an Arbitrary Drop-Down Control
    In This Topic

    Normally, True DBGrid for WinForms' 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 for WinForms control, to allow selection from a list of possible values. This is easy to do with True DBGrid for WinForms using virtually any Visual Studio 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 for WinForms fires the BeforeColEdit event each time the user wants to edit a cell. To override the default editing process, cancel C1TrueDBGrid'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:

      To write code in Visual Basic

      Visual Basic
      Copy 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
      

      To write code in C#

      C#
      Copy Code
      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;
      }
      
    3. Put code in the drop-down or combo box 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 box 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.