System.Windows.Forms.DataGridViewComboBoxColumn Class

Represents a column of System.Windows.Forms.DataGridViewComboBoxCell objects.

See Also: DataGridViewComboBoxColumn Members

Syntax

[System.ComponentModel.Designer("System.Windows.Forms.Design.DataGridViewComboBoxColumnDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.ComponentModel.Design.IDesigner")]
[System.Drawing.ToolboxBitmap("")]
public class DataGridViewComboBoxColumn : DataGridViewColumn

Remarks

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

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

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

One typical scenario is to bind the System.Windows.Forms.DataGridView control to a parent database table and bind the drop-down list to a related child table. For example, you might bind the System.Windows.Forms.DataGridView control to an Orders table that contains a ProductID column and set the column DataGridViewComboBoxColumn.DataSource property to a Products table that contains ProductID and ProductName columns. In this case, you would set the column DataGridViewColumn.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 DataGridViewComboBoxColumn.ValueMember property to "ProductID" and the DataGridViewComboBoxColumn.DisplayMember property to "ProductName".

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

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

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

The default sort mode for this column type is DataGridViewColumnSortMode.NotSortable.

Requirements

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Assembly Versions: 2.0.0.0
Since: .NET 2.0