The DropdownListCell is a built-in cell body that displays a drop-down list for single item selection. It extends DropdownCell, inheriting the drop-down panel infrastructure.

Namespace

using unvell.ReoGrid.CellTypes;

Create a Drop-down List Cell

var sheet = grid.CurrentWorksheet;

var dropdown = new DropdownListCell(
    "Apple", "Orange", "Banana", "Pear",
    "Pumpkin", "Cherry", "Coconut"
);

sheet["B2"] = dropdown;

Result: 196

Constructors

ConstructorDescription
DropdownListCell()Empty drop-down list
DropdownListCell(params string[] candidates)Create with string items
DropdownListCell(IEnumerable<string> candidates)Create with a collection
DropdownListCell(CellValueListSource source)Create from a cell value list source
DropdownListCell(ReferenceRange refRange)Create using a cell range as the data source

Using a Cell Range as Data Source

You can bind the drop-down list to a range of cells so the items update automatically:

// Items come from cells A1:A10
var dropdown = new DropdownListCell(sheet.Ranges["A1:A10"]);
sheet["C1"] = dropdown;

Using a Mutable List

Pass a List<string> to keep the items in sync:

var items = new List<string> { "Alpha", "Beta", "Gamma" };
var dropdown = new DropdownListCell(items);
sheet["B2"] = dropdown;

// Add items later — they appear in the dropdown
items.Add("Delta");

Properties

PropertyTypeDescription
SelectedItemobjectThe currently selected item
SelectedIndexintIndex of the selected item

Inherited from DropdownCell

PropertyTypeDefaultDescription
PullDownOnClickboolfalseOpen drop-down when cell is clicked (not just the button)
DropdownButtonSizeRGSize16x16Size of the drop-down button
DropdownButtonAutoHeightbooltrueAuto-adjust button height to match cell
IsDropdownboolWhether the drop-down panel is currently open (read-only)
DropdownPanelHeightint200Height of the drop-down panel in pixels
MinimumDropdownWidthint40Minimum width of the drop-down panel
MaximumDropdownWidthint1200Maximum width of the drop-down panel
DropdownControlControlThe platform control displayed in the panel
DropdownButtonStyleDropdownCellStyleVisual style of the drop-down button
PropertyTypeDescription
ArrowNormalColorSolidColor?Arrow color (uses worksheet text color if null)
ArrowDisableColorSolidColor?Arrow color when disabled

Events

EventDescription
SelectedItemChangedRaised when the selected item changes
DropdownOpenedRaised when the drop-down panel opens (inherited)
DropdownClosedRaised when the drop-down panel closes (inherited)
dropdown.SelectedItemChanged += (s, e) =>
{
    Console.WriteLine("Selected: " + dropdown.SelectedItem);
};

Since the drop-down list cell updates the cell data after an item is selected, the worksheet CellDataChanged event also fires:

sheet.CellDataChanged += (s, e) =>
{
    if (e.Cell.Position == new CellPosition("B2"))
    {
        Console.WriteLine("Selected: " + e.Cell.Data);
    }
};

Methods

MethodDescription
PushDown(bool forceCellEdit = true)Programmatically open the drop-down panel
PullUp()Close the drop-down panel
Clone()Create a copy of this cell body
// Open the dropdown programmatically
dropdown.PushDown();

// Close it
dropdown.PullUp();

Set Default Data

To set a default value or change the cell data:

sheet["B2"] = "Apple";

Result: 197

Change Drop-down Cell Size

Change Column Width

sheet.ColumnHeaders["B"].Width = 120;

198

Change Row Height

sheet.RowHeaders[1].Height = 40;

199-2

Merge Cells

sheet.MergeRange("B2:C2");

200

Set Padding Style

Shrink the drop-down control within the cell:

sheet.Cells["B2"].Style.Padding = new Padding(0, 0, 10, 0);

201

Change Drop-down Button Size

dropdown.DropdownButtonAutoHeight = false;
dropdown.DropdownButtonSize = new System.Drawing.Size(40, 15);

When DropdownButtonAutoHeight is true (default), the button stretches to fill the cell height. Set to false to use a fixed size:

202

Set Cell Border

A drop-down cell does not show borders by default. Add borders explicitly:

sheet.Ranges["B2"].BorderOutside = BorderStyle.GraySolid;

205

Open on Cell Click

By default, the dropdown opens only when the dropdown button is clicked. Set PullDownOnClick to open on any click within the cell:

dropdown.PullDownOnClick = true;

WPF Styling

On WPF, customize the list appearance through the Style property:

#if WPF
dropdown.Style.TextColor = SolidColor.DarkBlue;
dropdown.Style.BackColor = SolidColor.LightYellow;
dropdown.Style.FontSize = 14;
dropdown.Style.FontWeight = FontWeights.Bold;
dropdown.Style.HorizontalAlignment = HorizontalAlignment.Left;
dropdown.Style.Padding = new PaddingValue(4, 4, 4, 4);
#endif
PropertyTypeDescription
TextColorSolidColorText color in the list
BackColorSolidColorBackground color
FontFontFamilyFont family
FontWeightFontWeightFont weight
FontSizedoubleFont size
FontStyleFontStyleFont style (italic, etc.)
HorizontalAlignmentHorizontalAlignmentItem horizontal alignment
VerticalAlignmentVerticalAlignmentItem vertical alignment
PaddingPaddingValueItem padding
Reset()methodReset to default style
CopyFrom(style)methodCopy from another style

Draw a Custom Drop-down Button

Override OnPaintDropdownButton to customize the button appearance:

class MyDropdownListCell : DropdownListCell
{
    public MyDropdownListCell(params string[] items) : base(items) { }

    protected override void OnPaintDropdownButton(CellDrawingContext dc, Graphics.Rectangle buttonRect)
    {
        dc.Graphics.DrawAndFillRectangle(buttonRect, SolidColor.DeepSkyBlue, new SolidColor("#BEE7F1"));

        var centerPoint = new Graphics.Point(buttonRect.Left + buttonRect.Width / 2,
            buttonRect.Top + buttonRect.Height / 2);

        GraphicsToolkit.FillTriangle(dc.Graphics.PlatformGraphics, 9,
            centerPoint, GraphicsToolkit.TriangleDirection.Down,
            this.IsDropdown ? Pens.SkyBlue : Pens.DarkSlateBlue);
    }
}

203

Make a Custom Drop-down Cell

Create custom drop-down cells that place any control inside the panel by extending DropdownCell:

166

See How to Create a Custom Drop-down Cell.

Class Hierarchy

CellBody
  └─ DropdownCell (base — panel, button, open/close)
       └─ DropdownListBaseCell (candidate source)
            ├─ DropdownListCell (selection-only list)
            └─ ComboListCell (editable combo with auto-complete)
Was this article helpful?