This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft Edge More info about Internet Explorer and Microsoft Edge
public ref class DataGridViewComboBoxCell : System::Windows::Forms::DataGridViewCell
public class DataGridViewComboBoxCell : System.Windows.Forms.DataGridViewCell
type DataGridViewComboBoxCell = class
    inherit DataGridViewCell
Public Class DataGridViewComboBoxCell
Inherits DataGridViewCell
Inheritance
Object
DataGridViewComboBoxCell

Examples

The following code example demonstrates the DataGridViewComboBoxColumn class, which makes use of the DataGridViewComboBoxCell class. You can set the cell DataSource , ValueMember , and DisplayMember properties in the same way that the corresponding column properties are set in this example. This example is part of a larger example available in the DataGridViewComboBoxColumn class overview topic.

private: DataGridViewComboBoxColumn^ CreateComboBoxColumn() DataGridViewComboBoxColumn^ column = gcnew DataGridViewComboBoxColumn(); column->DataPropertyName = ColumnName::TitleOfCourtesy.ToString(); column->HeaderText = ColumnName::TitleOfCourtesy.ToString(); column->DropDownWidth = 160; column->Width = 90; column->MaxDropDownItems = 3; column->FlatStyle = FlatStyle::Flat; return column; private: void SetAlternateChoicesUsingDataSource(DataGridViewComboBoxColumn^ comboboxColumn) comboboxColumn->DataSource = RetrieveAlternativeTitles(); comboboxColumn->ValueMember = ColumnName::TitleOfCourtesy.ToString(); comboboxColumn->DisplayMember = comboboxColumn->ValueMember; private: DataTable^ RetrieveAlternativeTitles() return Populate("SELECT distinct TitleOfCourtesy FROM Employees"); String^ connectionString; private: DataTable^ Populate(String^ sqlCommand) SqlConnection^ northwindConnection = gcnew SqlConnection(connectionString); northwindConnection->Open(); SqlCommand^ command = gcnew SqlCommand(sqlCommand, northwindConnection); SqlDataAdapter^ adapter = gcnew SqlDataAdapter(); adapter->SelectCommand = command; DataTable^ table = gcnew DataTable(); adapter->Fill(table); return table; // Using an enum provides some abstraction between column index // and column name along with compile time checking, and gives // a handy place to store the column names. enum class ColumnName EmployeeID, LastName, FirstName, Title, TitleOfCourtesy, BirthDate, HireDate, Address, City, Region, PostalCode, Country, HomePhone, Extension, Photo, Notes, ReportsTo, PhotoPath, OutOfOffice private DataGridViewComboBoxColumn CreateComboBoxColumn() DataGridViewComboBoxColumn column = new DataGridViewComboBoxColumn(); column.DataPropertyName = ColumnName.TitleOfCourtesy.ToString(); column.HeaderText = ColumnName.TitleOfCourtesy.ToString(); column.DropDownWidth = 160; column.Width = 90; column.MaxDropDownItems = 3; column.FlatStyle = FlatStyle.Flat; return column; private void SetAlternateChoicesUsingDataSource(DataGridViewComboBoxColumn comboboxColumn) comboboxColumn.DataSource = RetrieveAlternativeTitles(); comboboxColumn.ValueMember = ColumnName.TitleOfCourtesy.ToString(); comboboxColumn.DisplayMember = comboboxColumn.ValueMember; private DataTable RetrieveAlternativeTitles() return Populate("SELECT distinct TitleOfCourtesy FROM Employees"); string connectionString = "Integrated Security=SSPI;Persist Security Info=False;" + "Initial Catalog=Northwind;Data Source=localhost"; private DataTable Populate(string sqlCommand) SqlConnection northwindConnection = new SqlConnection(connectionString); northwindConnection.Open(); SqlCommand command = new SqlCommand(sqlCommand, northwindConnection); SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = command; DataTable table = new DataTable(); table.Locale = System.Globalization.CultureInfo.InvariantCulture; adapter.Fill(table); return table; // Using an enum provides some abstraction between column index // and column name along with compile time checking, and gives // a handy place to store the column names. enum ColumnName EmployeeId, LastName, FirstName, Title, TitleOfCourtesy, BirthDate, HireDate, Address, City, Region, PostalCode, Country, HomePhone, Extension, Photo, Notes, ReportsTo, PhotoPath, OutOfOffice Private Function CreateComboBoxColumn() _ As DataGridViewComboBoxColumn Dim column As New DataGridViewComboBoxColumn() With column .DataPropertyName = ColumnName.TitleOfCourtesy.ToString() .HeaderText = ColumnName.TitleOfCourtesy.ToString() .DropDownWidth = 160 .Width = 90 .MaxDropDownItems = 3 .FlatStyle = FlatStyle.Flat End With Return column End Function Private Sub SetAlternateChoicesUsingDataSource( _ ByVal comboboxColumn As DataGridViewComboBoxColumn) With comboboxColumn .DataSource = RetrieveAlternativeTitles() .ValueMember = ColumnName.TitleOfCourtesy.ToString() .DisplayMember = .ValueMember End With End Sub Private Function RetrieveAlternativeTitles() As DataTable Return Populate( _ "SELECT distinct TitleOfCourtesy FROM Employees") End Function Private connectionString As String = _ "Integrated Security=SSPI;Persist Security Info=False;" _ & "Initial Catalog=Northwind;Data Source=localhost" Private Function Populate(ByVal sqlCommand As String) As DataTable Dim northwindConnection As New SqlConnection(connectionString) northwindConnection.Open() Dim command As New SqlCommand(sqlCommand, _ northwindConnection) Dim adapter As New SqlDataAdapter() adapter.SelectCommand = command Dim table As New DataTable() table.Locale = System.Globalization.CultureInfo.InvariantCulture adapter.Fill(table) Return table End Function ' Using an enum provides some abstraction between column index ' and column name along with compile time checking, and gives ' a handy place to store the column names. Enum ColumnName EmployeeId LastName FirstName Title TitleOfCourtesy BirthDate HireDate Address Region PostalCode Country HomePhone Extension Photo Notes ReportsTo PhotoPath OutOfOffice End Enum

Remarks

The DataGridViewComboBoxCell class is a specialized type of DataGridViewCell used to display a combo box control, which is an editing field combined with a list selection field. The currently selected DataGridViewComboBoxCell hosts a DataGridViewComboBoxEditingControl in which the user can change the cell's value, assuming the cell's ReadOnly property is set to false .

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 DataGridViewComboBoxColumn is the column type specialized to hold cells of this type. By default, the DataGridViewComboBoxColumn.CellTemplate is initialized to a new DataGridViewComboBoxCell . To pattern the cells within a column after an existing DataGridViewComboBoxCell , set the column's CellTemplate property to the cell to use as a pattern.

The cell-related properties of the column are wrappers for the similarly-named properties of the template cell. Changing the property values of the template cell will affect only cells based on the template that are added after the change. Changing the cell-related property values of the column, however, will update the template cell and all other cells in the column, and refresh the column display if necessary.

You will typically work with the DataGridViewComboBoxColumn type unless you want to override column values for specific cells. The guidelines described in the DataGridViewComboBoxColumn class overview topic for populating the drop-down list applies to both cell and column instances.

Notes to Inheritors

When you derive from DataGridViewComboBoxCell 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.

Gets the DataGridViewCell.DataGridViewCellAccessibleObject assigned to the DataGridViewCell .

(Inherited from DataGridViewCell )

Gets the current, formatted value of the cell, regardless of whether the cell is in edit mode and the value has not been committed.

(Inherited from DataGridViewCell )

Indicates whether the cell's row will be unshared when the cell's content is double-clicked.

(Inherited from DataGridViewCell )

Releases the unmanaged resources used by the DataGridViewCell and optionally releases the managed resources.

(Inherited from DataGridViewCell )

Returns the bounding rectangle that encloses the cell's content area using a default Graphics and cell style currently in effect for the cell.

(Inherited from DataGridViewCell )

Returns the current, formatted value of the cell, regardless of whether the cell is in edit mode and the value has not been committed.

(Inherited from DataGridViewCell )

Returns a value indicating the current state of the cell as inherited from the state of its row and column.

(Inherited from DataGridViewCell )

Indicates whether the parent row is unshared if the user presses a key while the focus is on the cell.

(Inherited from DataGridViewCell )

Indicates whether a row will be unshared if a key is pressed while a cell in the row has focus.

(Inherited from DataGridViewCell )

Indicates whether the parent row is unshared when the user releases a key while the focus is on the cell.

(Inherited from DataGridViewCell )

Indicates whether a row will be unshared if the user clicks a mouse button while the pointer is on a cell in the row.

(Inherited from DataGridViewCell )

Indicates whether a row will be unshared when the user holds down a mouse button while the pointer is on a cell in the row.

(Inherited from DataGridViewCell )

Indicates whether a row will be unshared when the mouse pointer moves over a cell in the row.

(Inherited from DataGridViewCell )

Indicates whether a row will be unshared when the mouse pointer moves over a cell in the row.

(Inherited from DataGridViewCell )

Indicates whether a row will be unshared when the user releases a mouse button while the pointer is on a cell in the row.

(Inherited from DataGridViewCell ) Paint(Graphics, Rectangle, Rectangle, Int32, DataGridViewElementStates, Object, Object, String, DataGridViewCellStyle, DataGridViewAdvancedBorderStyle, DataGridViewPaintParts)

Paints the current DataGridViewComboBoxCell .

Sets the location and size of the editing control hosted by a cell in the DataGridView control.

(Inherited from DataGridViewCell )

Sets the location and size of the editing panel hosted by the cell, and returns the normal bounds of the editing control within the editing panel.

(Inherited from DataGridViewCell )