DataGridViewEditingControlShowingEventArgs.Control 属性

定义

向用户显示的用于编辑选定单元格的值的控件。

C#
public System.Windows.Forms.Control Control { get; }

属性值

一个 Control,它显示一个区域供用户输入或更改选定单元格的值。

示例

下面的代码示例演示了此属性的用法。 在此示例中, DataGridView.EditingControlShowing 事件处理程序为 DataGridViewComboBoxEditingControl 事件添加处理程序。 将编辑控件强制转换为 以 ComboBox 处理 ComboBox.SelectedIndexChanged 事件。

C#
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;
}

注解

若要自定义控件的显示特征,请设置 属性返回 CellStyle 的 对象的属性,而不是设置 属性返回的控件的属性 Control

适用于

产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅