This documentation is archived and is not being maintained.

DataGridViewComboBoxColumn Class

Represents a column of DataGridViewComboBoxCell objects.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public class DataGridViewComboBoxColumn extends DataGridViewColumn

The DataGridViewComboBoxColumn class is a specialized type of DataGridViewColumn used to logically host cells that enable users to select values from a list of choices. A DataGridViewComboBoxColumn has an associated DataGridViewComboBoxCell in every DataGridViewRow that intersects it.

You can populate the cells manually by setting their Value properties. Alternatively, you can bind the column to the data source indicated by the DataGridView.DataSource property. If the DataGridView is bound to a database table, set the column DataPropertyName property to the name of a column in the table. If the DataGridView is bound to a collection of objects, set the DataPropertyName property to the name of an object property.

You can populate the column drop-down list manually by adding values to the Items collection. Alternatively, you can bind the drop-down list to its own data source by setting the column DataSource property. If the values are objects in a collection or records in a database table, you must also set the DisplayMember and ValueMember properties. The DisplayMember property indicates which object property or database column provides the values that are displayed in the drop-down list. The ValueMember property indicates which object property or database column is used to set the cell Value property.

One typical scenario is to bind the DataGridView control to a parent database table and bind the drop-down list to a related child table. For example, you might bind the DataGridView control to an Orders table that contains a ProductID column and set the column DataSource property to a Products table that contains ProductID and ProductName columns. In this case, you would set the column DataPropertyName property to "ProductID" to populate its cell values from the Orders.ProductID column. However, to display the actual product names in the cells and the drop-down list, you would map these values to the Products table by setting the ValueMember property to "ProductID" and the DisplayMember property to "ProductName".

The drop-down list values (or the values indicated by the ValueMember property) must include the actual cell values or the DataGridView control will throw an exception.

Setting the column DataSource, DisplayMember, and ValueMember properties automatically sets the corresponding properties of all cells in the column including the CellTemplate. To override these property values for specific cells, set the column property first, and then set the cell properties.

Unlike the ComboBox control, the DataGridViewComboBoxCell does not have SelectedIndex and SelectedValue properties. Instead, selecting a value from a drop-down list sets the cell Value property.

The default sort mode for this column type is NotSortable.

Notes to Inheritors:

When you derive from DataGridViewComboBoxColumn and add new properties to the derived class, be sure to override the Clone method to copy the new properties during cloning operations. You should also call the base class's Clone method so that the properties of the base class are copied to the new cell.

The following code example demonstrates how to use a DataGridViewComboBoxColumn to aid in entering data into the TitleOfCourtesy column.

No code example is currently available or this language may not be supported.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0