DataGridViewCell.InitializeEditingControl Metoda

Definice

Inicializuje ovládací prvek použitý k úpravě buňky.

public:
 virtual void InitializeEditingControl(int rowIndex, System::Object ^ initialFormattedValue, System::Windows::Forms::DataGridViewCellStyle ^ dataGridViewCellStyle);
public virtual void InitializeEditingControl (int rowIndex, object initialFormattedValue, System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle);
abstract member InitializeEditingControl : int * obj * System.Windows.Forms.DataGridViewCellStyle -> unit
override this.InitializeEditingControl : int * obj * System.Windows.Forms.DataGridViewCellStyle -> unit
Public Overridable Sub InitializeEditingControl (rowIndex As Integer, initialFormattedValue As Object, dataGridViewCellStyle As DataGridViewCellStyle)

Parametry

rowIndex
Int32

Index řádku založeného na nule umístění buňky.

initialFormattedValue
Object

Hodnota Object , která představuje hodnotu zobrazenou buňkou při spuštění úprav.

dataGridViewCellStyle
DataGridViewCellStyle

A DataGridViewCellStyle , který představuje styl buňky.

Výjimky

Není přidruženo DataGridView nebo pokud je k dispozici, nemá přidružený ovládací prvek pro úpravy.

Příklady

Následující příklad kódu ukazuje, jak přepsat metodu InitializeEditingControl v jednoduché třídě, která je odvozena z DataGridViewTextBoxCell třídy. Tento příklad je součástí rozsáhlejšího příkladu kódu uvedeného v článku Postupy: Hostitelské ovládací prvky v buňkách model Windows Forms DataGridView.

public class CalendarCell : DataGridViewTextBoxCell
{

    public CalendarCell()
        : base()
    {
        // Use the short date format.
        this.Style.Format = "d";
    }

    public override void InitializeEditingControl(int rowIndex, object 
        initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
    {
        // Set the value of the editing control to the current cell value.
        base.InitializeEditingControl(rowIndex, initialFormattedValue, 
            dataGridViewCellStyle);
        CalendarEditingControl ctl = 
            DataGridView.EditingControl as CalendarEditingControl;
        // Use the default row value when Value property is null.
        if (this.Value == null)
        {
            ctl.Value = (DateTime)this.DefaultNewRowValue;
        }
        else
        {
            ctl.Value = (DateTime)this.Value;
        }
    }

    public override Type EditType
    {
        get
        {
            // Return the type of the editing control that CalendarCell uses.
            return typeof(CalendarEditingControl);
        }
    }

    public override Type ValueType
    {
        get
        {
            // Return the type of the value that CalendarCell contains.
            
            return typeof(DateTime);
        }
    }

    public override object DefaultNewRowValue
    {
        get
        {
            // Use the current date and time as the default value.
            return DateTime.Now;
        }
    }
}
Public Class CalendarCell
    Inherits DataGridViewTextBoxCell

    Public Sub New()
        ' Use the short date format.
        Me.Style.Format = "d"
    End Sub

    Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _
        ByVal initialFormattedValue As Object, _
        ByVal dataGridViewCellStyle As DataGridViewCellStyle)

        ' Set the value of the editing control to the current cell value.
        MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _
            dataGridViewCellStyle)

        Dim ctl As CalendarEditingControl = _
            CType(DataGridView.EditingControl, CalendarEditingControl)

        ' Use the default row value when Value property is null.
        If (Me.Value Is Nothing) Then
            ctl.Value = CType(Me.DefaultNewRowValue, DateTime)
        Else
            ctl.Value = CType(Me.Value, DateTime)
        End If
    End Sub

    Public Overrides ReadOnly Property EditType() As Type
        Get
            ' Return the type of the editing control that CalendarCell uses.
            Return GetType(CalendarEditingControl)
        End Get
    End Property

    Public Overrides ReadOnly Property ValueType() As Type
        Get
            ' Return the type of the value that CalendarCell contains.
            Return GetType(DateTime)
        End Get
    End Property

    Public Overrides ReadOnly Property DefaultNewRowValue() As Object
        Get
            ' Use the current date and time as the default value.
            Return DateTime.Now
        End Get
    End Property

End Class

Poznámky

Jako technika optimalizace se obvykle všechny buňky stejného typu a ve stejné DataGridView sdílené složce sdílejí jeden hostovaný ovládací prvek pro úpravy. Než ale ovládací prvek použije buňka, musí být inicializován metodou InitializeEditingControl . Při prvním zavolání tato metoda přidá ovládací prvek do seznamu úprav ovládacích prvků v nadřazené DataGridView. Inicializuje také některé vizuální vlastnosti buňky. Například nastaví barvu pozadí oblasti pro úpravy tak, InitializeEditingControl aby odpovídal zadanému parametru stylu buňky. Následná volání, která nic InitializeEditingControl neudělá.

Odvozené třídy používají tuto metodu Control k hostování instance třídy odpovídající jejich typu. Například tabulka, která obsahuje jeden nebo více DataGridViewTextBoxCell objektů volá tuto metodu pro přidání jednoho TextBox ovládacího prvku pro úpravy do vlastnictví DataGridView.

Platí pro

Viz také