Export (0) Print
Expand All
4 out of 8 rated this helpful - Rate this topic

DataGridViewComboBoxEditingControl Class

Represents the hosted combo box control in a DataGridViewComboBoxCell.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
[ComVisibleAttribute(true)]
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)]
public class DataGridViewComboBoxEditingControl : ComboBox, 
	IDataGridViewEditingControl

The DataGridViewComboBoxEditingControl class is a specialized type of ComboBox that implements the IDataGridViewEditingControl interface, allowing it to be hosted in a DataGridViewComboBoxCell when the cell is in edit mode. When a DataGridViewComboBoxCell is in edit mode, you can retrieve the DataGridViewComboBoxEditingControl through the EditingControl property of the DataGridView control.

Handle the EditingControlShowing event to perform custom initialization of the editing control when a cell enters edit mode. To customize the display characteristics of the control, set the properties of the object returned by the DataGridViewEditingControlShowingEventArgs.CellStyle property rather than setting the properties of the control returned by the DataGridViewEditingControlShowingEventArgs.Control property.

The following code example demonstrates how to use an DataGridView.EditingControlShowing event handler to add a handler for a DataGridViewComboBoxEditingControl event. In the example, the editing control is cast to a ComboBox to handle the ComboBox.SelectedIndexChanged event.

private DataGridView dataGridView1 = new DataGridView();

private void AddColorColumn()
{
    DataGridViewComboBoxColumn comboBoxColumn =
        new DataGridViewComboBoxColumn();
    comboBoxColumn.Items.AddRange(
        Color.Red, Color.Yellow, Color.Green, Color.Blue);
    comboBoxColumn.ValueType = typeof(Color);
    dataGridView1.Columns.Add(comboBoxColumn);
    dataGridView1.EditingControlShowing +=
        new DataGridViewEditingControlShowingEventHandler(
        dataGridView1_EditingControlShowing);
}

private void dataGridView1_EditingControlShowing(object sender,
    DataGridViewEditingControlShowingEventArgs e)
{
    ComboBox combo = e.Control as ComboBox;
    if (combo != null)
    {
        // Remove an existing event-handler, if present, to avoid  
        // adding multiple handlers when the editing control is reused.
        combo.SelectedIndexChanged -=
            new EventHandler(ComboBox_SelectedIndexChanged);

        // Add the event handler. 
        combo.SelectedIndexChanged +=
            new EventHandler(ComboBox_SelectedIndexChanged);
    }
}

private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
    ((ComboBox)sender).BackColor = (Color)((ComboBox)sender).SelectedItem;
}
System.Object
  System.MarshalByRefObject
    System.ComponentModel.Component
      System.Windows.Forms.Control
        System.Windows.Forms.ListControl
          System.Windows.Forms.ComboBox
            System.Windows.Forms.DataGridViewComboBoxEditingControl
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

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.