# Dropdown Controls

## Content



**True DBGrid** provides a wide variety of built-in controls to implement virtually any kind of drop-down cell-editing interface. For instance, the [ValueItems](/componentone/docs/win/online-truedbgrid/) object and its collection of [ValueItem](/componentone/docs/win/online-truedbgrid/) objects can be used to provide a simple pick list, or the [C1TrueDBDropDown](/componentone/docs/win/online-truedbgrid/) control can be used to implement a data-aware multicolumn combo box.

## Built-In Combo Box

The [C1DataColumn](/componentone/docs/win/online-truedbgrid/) object's [ValueItems](/componentone/docs/win/online-truedbgrid/) object optionally provides a built-in combo box interface that works in concert with its automatic data translation features. By default, the [Presentation](/componentone/docs/win/online-truedbgrid/) property is set to [PresentationEnum.Normal](/componentone/docs/win/online-truedbgrid/), and the usual cell editing behavior is in effect for textual data. However, if the [Presentation](/componentone/docs/win/online-truedbgrid/) property is set to either [PresentationEnum.ComboBox](/componentone/docs/win/online-truedbgrid/) or [PresentationEnum.SortedComboBox](/componentone/docs/win/online-truedbgrid/), then cells in the affected column display the in-cell button upon receiving focus. When the user clicks the in-cell button, a drop-down combo box appears. The drop-down combo box contains one item for each member of the [ValueItemCollection](/componentone/docs/win/online-truedbgrid/) object. If the collection's [Translate](/componentone/docs/win/online-truedbgrid/) property is **True**, then the [DisplayValue](/componentone/docs/win/online-truedbgrid/) text is used for the combo box items; if it is **False**, then the [Value](/componentone/docs/win/online-truedbgrid/) text is used.

**True DBGrid** automatically sizes the drop-down combo box to fit the width of the column in which it is displayed. The height of the combo box is determined by the number of items in the collection and the [MaxComboItems](/componentone/docs/win/online-truedbgrid/) property. If the number of items is less than or equal to [MaxComboItems](/componentone/docs/win/online-truedbgrid/), which has a default value of 5, and then all value items will be shown. If the number of items exceeds [MaxComboItems](/componentone/docs/win/online-truedbgrid/), only [MaxComboItems](/componentone/docs/win/online-truedbgrid/) will be shown, but a scroll bar will appear at the right edge of the combo box to allow users to bring the other items into view.

The [ComboSelect](/componentone/docs/win/online-truedbgrid/) event is fired when the user selects an item from the built-in combo box. This event is useful for determining the contents of the cell before the user exits edit mode.

Since the items displayed in the built-in combo box are often the only allowable values for the underlying data source, you may need to prevent your users from typing in the cell after making a selection. By setting the [C1DisplayColumn](/componentone/docs/win/online-truedbgrid/) property [DropDownList](/componentone/docs/win/online-truedbgrid/) equal to **True**, the attached [C1TrueDBDropDown](/componentone/docs/win/online-truedbgrid/) control will now be limited to use only as a list box. No new values or changes will be allowed in the drop-down and so the underlying database cannot be updated with false information.

## TrueDBDropDown Control

The built-in drop-down combo box described in the preceding example is most useful when the allowable values are both known in advance and relatively few in number. A large collection of [ValueItem](/componentone/docs/win/online-truedbgrid/) objects can be unwieldy to maintain in the designer, and requires substantial coding to set up. Moreover, the built-in combo box cannot be bound to a data control and be populated automatically.

Using the techniques outlined later in this chapter, set up a secondary [C1TrueDBGrid](/componentone/docs/win/online-truedbgrid/) control to be used as a drop-down. However, to display a list of values from another data source, the [C1TrueDBDropDown](/componentone/docs/win/online-truedbgrid/) control offers a more elegant solution, as it was designed explicitly for that purpose and can be set up entirely at design time.<br />

![](https://cdn.mescius.io/document-site-files/images/a1b176e5-6638-4b1f-a786-e41a817558bb/images/truedbdropdwon.png)

To use the drop-down control, set the [DropDown](/componentone/docs/win/online-truedbgrid/) property of a grid column to the [C1TrueDBDropDown](/componentone/docs/win/online-truedbgrid/) control either in the designer or in code. At run time, when the user clicks the in-cell button for that column, the [C1TrueDBDropDown](/componentone/docs/win/online-truedbgrid/) control will appear below the grid's current cell. If the user selects an item from the drop-down control, the grid's current cell is updated.

Since the [C1TrueDBDropDown](/componentone/docs/win/online-truedbgrid/) control is a subset of [C1TrueDBGrid](/componentone/docs/win/online-truedbgrid/), it shares many of the same properties, methods, and events. However, the following two properties are specific to the [C1TrueDBDropDown](/componentone/docs/win/online-truedbgrid/) control:

| Property | Description |
| --- | --- |
| [ValueMember](/componentone/docs/win/online-truedbgrid/) | This property specifies the drop-down column used to update the associated grid column when a selection is made. |
| [DisplayMember](/componentone/docs/win/online-truedbgrid/) | This property specifies the name of the drop-down column to be used for incremental search. |
| [AllowSizing](/componentone/docs/win/online-truedbgrid/) | This property specifies a value indicating whether dropdown resizing is allowed or not. |

When a [C1TrueDBDropDown](/componentone/docs/win/online-truedbgrid/) control becomes visible, its [DropDownOpen](/componentone/docs/win/online-truedbgrid/) event fires. Similarly, when the user makes a selection or the control loses focus, its [DropDownClose](/componentone/docs/win/online-truedbgrid/) event fires.

## Arbitrary Drop-Down Control

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 Studio or third-party control.

![](https://cdn.mescius.io/document-site-files/images/a1b176e5-6638-4b1f-a786-e41a817558bb/images/listbox-truedbgrid.png)

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](/componentone/docs/win/online-truedbgrid/) 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](/componentone/docs/win/online-truedbgrid/) 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](/componentone/docs/win/online-truedbgrid/) 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](/componentone/docs/win/online-truedbgrid/) (column only, used with multiple line displays), [Width](/componentone/docs/win/online-truedbgrid/) (column only), and [RowHeight](/componentone/docs/win/online-truedbgrid/)(grid only). The method is [RowTop](/componentone/docs/win/online-truedbgrid/) (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:
    
    ```csharp
    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](/componentone/docs/win/online-truedbgrid/) or [Value](/componentone/docs/win/online-truedbgrid/) property of the column being edited.

This method does not work, however, when the grid's [MarqueeStyle](/componentone/docs/win/online-truedbgrid/) property is set to the value of [MarqueeEnum.FloatingEditor](/componentone/docs/win/online-truedbgrid/). When the floating editor marquee is used, the [BeforeColEdit](/componentone/docs/win/online-truedbgrid/) 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](/componentone/docs/win/online-truedbgrid/) settings, see [Highlighting the Current Row or Cell](/componentone/docs/win/online-truedbgrid/customization/highlighting-current-row-cell).

## Built-In Column Button

An alternative way to drop down a control from a cell is to use **True DBGrid for WinForms**' built-in column button feature. If a column's [Button](/componentone/docs/win/online-truedbgrid/) property is set to **True**, a button will be displayed at the right edge of the current cell when it is in that column. Clicking the button fires the grid's [ButtonClick](/componentone/docs/win/online-truedbgrid/) event. Drop-down a control from the cell using code inside the [ButtonClick](/componentone/docs/win/online-truedbgrid/) event. Also use this event to trigger any action or calculation inside the cell.