DataGridViewEditingControlShowingEventArgs.Control 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
顯示給使用者用於編輯選定儲存格值的控制項。
public:
property System::Windows::Forms::Control ^ Control { System::Windows::Forms::Control ^ get(); };
public System.Windows.Forms.Control Control { get; }
member this.Control : System.Windows.Forms.Control
Public ReadOnly Property Control As Control
屬性值
Control,顯示讓使用者輸入或變更選定儲存格值的區域。
範例
下列程式碼範例說明此屬性的使用。 在此範例中 DataGridView.EditingControlShowing ,事件處理常式會新增事件的處理常式 DataGridViewComboBoxEditingControl 。 編輯控制項會轉換成 ComboBox ,以處理 ComboBox.SelectedIndexChanged 事件。
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;
}
Private WithEvents dataGridView1 As New DataGridView()
Private Sub AddColorColumn()
Dim comboBoxColumn As New DataGridViewComboBoxColumn()
comboBoxColumn.Items.AddRange( _
Color.Red, Color.Yellow, Color.Green, Color.Blue)
comboBoxColumn.ValueType = GetType(Color)
dataGridView1.Columns.Add(comboBoxColumn)
End Sub
Private Sub dataGridView1_EditingControlShowing(ByVal sender As Object, _
ByVal e As DataGridViewEditingControlShowingEventArgs) _
Handles dataGridView1.EditingControlShowing
Dim combo As ComboBox = CType(e.Control, ComboBox)
If (combo IsNot Nothing) Then
' Remove an existing event-handler, if present, to avoid
' adding multiple handlers when the editing control is reused.
RemoveHandler combo.SelectedIndexChanged, _
New EventHandler(AddressOf ComboBox_SelectedIndexChanged)
' Add the event handler.
AddHandler combo.SelectedIndexChanged, _
New EventHandler(AddressOf ComboBox_SelectedIndexChanged)
End If
End Sub
Private Sub ComboBox_SelectedIndexChanged( _
ByVal sender As Object, ByVal e As EventArgs)
Dim comboBox1 As ComboBox = CType(sender, ComboBox)
comboBox1.BackColor = _
CType(CType(sender, ComboBox).SelectedItem, Color)
End Sub
備註
若要自訂控制項的顯示特性,請設定 屬性所 CellStyle 傳回之物件的屬性,而不是設定 屬性所 Control 傳回之控制項的屬性。