Redigera

Dela via


DataGridViewComboBoxCell Class

Definition

Displays a combo box in a DataGridView control.

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
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
    City
    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.

Constructors

DataGridViewComboBoxCell()

Initializes a new instance of the DataGridViewComboBoxCell class.

Properties

AccessibilityObject

Gets the DataGridViewCell.DataGridViewCellAccessibleObject assigned to the DataGridViewCell.

(Inherited from DataGridViewCell)
AutoComplete

Gets or sets a value indicating whether the cell will match the characters being entered in the cell with a selection from the drop-down list.

ColumnIndex

Gets the column index for this cell.

(Inherited from DataGridViewCell)
ContentBounds

Gets the bounding rectangle that encloses the cell's content area.

(Inherited from DataGridViewCell)
ContextMenuStrip

Gets or sets the shortcut menu associated with the cell.

(Inherited from DataGridViewCell)
DataGridView

Gets the DataGridView control associated with this element.

(Inherited from DataGridViewElement)
DataSource

Gets or sets the data source whose data contains the possible selections shown in the drop-down list.

DefaultNewRowValue

Gets the default value for a cell in the row for new records.

(Inherited from DataGridViewCell)
Displayed

Gets a value that indicates whether the cell is currently displayed on-screen.

(Inherited from DataGridViewCell)
DisplayMember

Gets or sets a string that specifies where to gather selections to display in the drop-down list.

DisplayStyle

Gets or sets a value that determines how the combo box is displayed when it is not in edit mode.

DisplayStyleForCurrentCellOnly

Gets or sets a value indicating whether the DisplayStyle property value applies to the cell only when it is the current cell in the DataGridView control.

DropDownWidth

Gets or sets the width of the of the drop-down list portion of a combo box.

EditedFormattedValue

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)
EditType

Gets the type of the cell's hosted editing control.

ErrorIconBounds

Gets the bounds of the error icon for the cell.

(Inherited from DataGridViewCell)
ErrorText

Gets or sets the text describing an error condition associated with the cell.

(Inherited from DataGridViewCell)
FlatStyle

Gets or sets the flat style appearance of the cell.

FormattedValue

Gets the value of the cell as formatted for display.

(Inherited from DataGridViewCell)
FormattedValueType

Gets the class type of the formatted value associated with the cell.

Frozen

Gets a value indicating whether the cell is frozen.

(Inherited from DataGridViewCell)
HasStyle

Gets a value indicating whether the Style property has been set.

(Inherited from DataGridViewCell)
InheritedState

Gets the current state of the cell as inherited from the state of its row and column.

(Inherited from DataGridViewCell)
InheritedStyle

Gets the style currently applied to the cell.

(Inherited from DataGridViewCell)
IsInEditMode

Gets a value indicating whether this cell is currently being edited.

(Inherited from DataGridViewCell)
Items

Gets the objects that represent the selection displayed in the drop-down list.

MaxDropDownItems

Gets or sets the maximum number of items shown in the drop-down list.

OwningColumn

Gets the column that contains this cell.

(Inherited from DataGridViewCell)
OwningRow

Gets the row that contains this cell.

(Inherited from DataGridViewCell)
PreferredSize

Gets the size, in pixels, of a rectangular area into which the cell can fit.

(Inherited from DataGridViewCell)
ReadOnly

Gets or sets a value indicating whether the cell's data can be edited.

(Inherited from DataGridViewCell)
Resizable

Gets a value indicating whether the cell can be resized.

(Inherited from DataGridViewCell)
RowIndex

Gets the index of the cell's parent row.

(Inherited from DataGridViewCell)
Selected

Gets or sets a value indicating whether the cell has been selected.

(Inherited from DataGridViewCell)
Size

Gets the size of the cell.

(Inherited from DataGridViewCell)
Sorted

Gets or sets a value indicating whether the items in the combo box are automatically sorted.

State

Gets the user interface (UI) state of the element.

(Inherited from DataGridViewElement)
Style

Gets or sets the style for the cell.

(Inherited from DataGridViewCell)
Tag

Gets or sets the object that contains supplemental data about the cell.

(Inherited from DataGridViewCell)
ToolTipText

Gets or sets the ToolTip text associated with this cell.

(Inherited from DataGridViewCell)
Value

Gets or sets the value associated with this cell.

(Inherited from DataGridViewCell)
ValueMember

Gets or sets a string that specifies where to gather the underlying values used in the drop-down list.

ValueType

Gets or sets the data type of the values in the cell.

Visible

Gets a value indicating whether the cell is in a row or column that has been hidden.

(Inherited from DataGridViewCell)

Methods

AdjustCellBorderStyle(DataGridViewAdvancedBorderStyle, DataGridViewAdvancedBorderStyle, Boolean, Boolean, Boolean, Boolean)

Modifies the input cell border style according to the specified criteria.

(Inherited from DataGridViewCell)
BorderWidths(DataGridViewAdvancedBorderStyle)

Returns a Rectangle that represents the widths of all the cell margins.

(Inherited from DataGridViewCell)
ClickUnsharesRow(DataGridViewCellEventArgs)

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

(Inherited from DataGridViewCell)
Clone()

Creates an exact copy of this cell.

ContentClickUnsharesRow(DataGridViewCellEventArgs)

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

(Inherited from DataGridViewCell)
ContentDoubleClickUnsharesRow(DataGridViewCellEventArgs)

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

(Inherited from DataGridViewCell)
CreateAccessibilityInstance()

Creates a new AccessibleObject for this DataGridViewComboBoxCell instance.

CreateAccessibilityInstance()

Creates a new accessible object for the DataGridViewCell.

(Inherited from DataGridViewCell)
DetachEditingControl()

Removes the cell's editing control from the DataGridView.

Dispose()

Releases all resources used by the DataGridViewCell.

(Inherited from DataGridViewCell)
Dispose(Boolean)

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

(Inherited from DataGridViewCell)
DoubleClickUnsharesRow(DataGridViewCellEventArgs)

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

(Inherited from DataGridViewCell)
EnterUnsharesRow(Int32, Boolean)

Indicates whether the parent row will be unshared when the focus moves to the cell.

(Inherited from DataGridViewCell)
Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetClipboardContent(Int32, Boolean, Boolean, Boolean, Boolean, String)

Retrieves the formatted value of the cell to copy to the Clipboard.

(Inherited from DataGridViewCell)
GetContentBounds(Graphics, DataGridViewCellStyle, Int32)

Returns the bounding rectangle that encloses the cell's content area, which is calculated using the specified Graphics and cell style.

GetContentBounds(Int32)

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)
GetEditedFormattedValue(Int32, DataGridViewDataErrorContexts)

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)
GetErrorIconBounds(Graphics, DataGridViewCellStyle, Int32)

Returns the bounding rectangle that encloses the cell's error icon, if one is displayed.

GetErrorText(Int32)

Returns a string that represents the error for the cell.

(Inherited from DataGridViewCell)
GetFormattedValue(Object, Int32, DataGridViewCellStyle, TypeConverter, TypeConverter, DataGridViewDataErrorContexts)

Gets the formatted value of the cell's data.

GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetInheritedContextMenuStrip(Int32)

Gets the inherited shortcut menu for the current cell.

(Inherited from DataGridViewCell)
GetInheritedState(Int32)

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

(Inherited from DataGridViewCell)
GetInheritedStyle(DataGridViewCellStyle, Int32, Boolean)

Gets the style applied to the cell.

(Inherited from DataGridViewCell)
GetPreferredSize(Graphics, DataGridViewCellStyle, Int32, Size)

Calculates the preferred size, in pixels, of the cell.

GetSize(Int32)

Gets the size of the cell.

(Inherited from DataGridViewCell)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
GetValue(Int32)

Gets the value of the cell.

(Inherited from DataGridViewCell)
InitializeEditingControl(Int32, Object, DataGridViewCellStyle)

Attaches and initializes the hosted editing control.

KeyDownUnsharesRow(KeyEventArgs, Int32)

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

(Inherited from DataGridViewCell)
KeyEntersEditMode(KeyEventArgs)

Determines if edit mode should be started based on the given key.

KeyPressUnsharesRow(KeyPressEventArgs, Int32)

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

(Inherited from DataGridViewCell)
KeyUpUnsharesRow(KeyEventArgs, Int32)

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

(Inherited from DataGridViewCell)
LeaveUnsharesRow(Int32, Boolean)

Indicates whether a row will be unshared when the focus leaves a cell in the row.

(Inherited from DataGridViewCell)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
MouseClickUnsharesRow(DataGridViewCellMouseEventArgs)

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)
MouseDoubleClickUnsharesRow(DataGridViewCellMouseEventArgs)

Indicates whether a row will be unshared if the user double-clicks a cell in the row.

(Inherited from DataGridViewCell)
MouseDownUnsharesRow(DataGridViewCellMouseEventArgs)

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)
MouseEnterUnsharesRow(Int32)

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

(Inherited from DataGridViewCell)
MouseLeaveUnsharesRow(Int32)

Indicates whether a row will be unshared when the mouse pointer leaves the row.

(Inherited from DataGridViewCell)
MouseMoveUnsharesRow(DataGridViewCellMouseEventArgs)

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

(Inherited from DataGridViewCell)
MouseUpUnsharesRow(DataGridViewCellMouseEventArgs)

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)
OnClick(DataGridViewCellEventArgs)

Called when the cell is clicked.

(Inherited from DataGridViewCell)
OnContentClick(DataGridViewCellEventArgs)

Called when the cell's contents are clicked.

(Inherited from DataGridViewCell)
OnContentDoubleClick(DataGridViewCellEventArgs)

Called when the cell's contents are double-clicked.

(Inherited from DataGridViewCell)
OnDataGridViewChanged()

Called when the DataGridView property of the cell changes.

OnDoubleClick(DataGridViewCellEventArgs)

Called when the cell is double-clicked.

(Inherited from DataGridViewCell)
OnEnter(Int32, Boolean)

Called when the focus moves to a cell.

OnKeyDown(KeyEventArgs, Int32)

Called when a character key is pressed while the focus is on a cell.

(Inherited from DataGridViewCell)
OnKeyPress(KeyPressEventArgs, Int32)

Called when a key is pressed while the focus is on a cell.

(Inherited from DataGridViewCell)
OnKeyUp(KeyEventArgs, Int32)

Called when a character key is released while the focus is on a cell.

(Inherited from DataGridViewCell)
OnLeave(Int32, Boolean)

Called when the focus moves from a cell.

OnMouseClick(DataGridViewCellMouseEventArgs)

Called when the user clicks a mouse button while the pointer is on a cell.

OnMouseDoubleClick(DataGridViewCellMouseEventArgs)

Called when the user double-clicks a mouse button while the pointer is on a cell.

(Inherited from DataGridViewCell)
OnMouseDown(DataGridViewCellMouseEventArgs)

Called when the user holds down a mouse button while the pointer is on a cell.

(Inherited from DataGridViewCell)
OnMouseEnter(Int32)

Called when the mouse pointer moves over a cell.

OnMouseLeave(Int32)

Called when the mouse pointer leaves the cell.

OnMouseMove(DataGridViewCellMouseEventArgs)

Called when the mouse pointer moves within a cell.

OnMouseUp(DataGridViewCellMouseEventArgs)

Called when the user releases a mouse button while the pointer is on a cell.

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

Paints the current DataGridViewComboBoxCell.

PaintBorder(Graphics, Rectangle, Rectangle, DataGridViewCellStyle, DataGridViewAdvancedBorderStyle)

Paints the border of the current DataGridViewCell.

(Inherited from DataGridViewCell)
PaintErrorIcon(Graphics, Rectangle, Rectangle, String)

Paints the error icon of the current DataGridViewCell.

(Inherited from DataGridViewCell)
ParseFormattedValue(Object, DataGridViewCellStyle, TypeConverter, TypeConverter)

Converts a value formatted for display to an actual cell value.

PositionEditingControl(Boolean, Boolean, Rectangle, Rectangle, DataGridViewCellStyle, Boolean, Boolean, Boolean, Boolean)

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

(Inherited from DataGridViewCell)
PositionEditingPanel(Rectangle, Rectangle, DataGridViewCellStyle, Boolean, Boolean, Boolean, Boolean)

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)
RaiseCellClick(DataGridViewCellEventArgs)

Raises the CellClick event.

(Inherited from DataGridViewElement)
RaiseCellContentClick(DataGridViewCellEventArgs)

Raises the CellContentClick event.

(Inherited from DataGridViewElement)
RaiseCellContentDoubleClick(DataGridViewCellEventArgs)

Raises the CellContentDoubleClick event.

(Inherited from DataGridViewElement)
RaiseCellValueChanged(DataGridViewCellEventArgs)

Raises the CellValueChanged event.

(Inherited from DataGridViewElement)
RaiseDataError(DataGridViewDataErrorEventArgs)

Raises the DataError event.

(Inherited from DataGridViewElement)
RaiseMouseWheel(MouseEventArgs)

Raises the MouseWheel event.

(Inherited from DataGridViewElement)
SetValue(Int32, Object)

Sets the value of the cell.

(Inherited from DataGridViewCell)
ToString()

Returns a string that describes the current object.

Applies to

See also