DataGridViewCell.InitializeEditingControl Metod

Definition

Initierar kontrollen som används för att redigera cellen.

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

Parametrar

rowIndex
Int32

Det nollbaserade radindexet för cellens plats.

initialFormattedValue
Object

En Object som representerar det värde som visas av cellen när redigeringen startas.

dataGridViewCellStyle
DataGridViewCellStyle

En DataGridViewCellStyle som representerar cellens formatmall.

Undantag

Det finns ingen associerad DataGridView eller om en finns, den har ingen associerad redigeringskontroll.

Exempel

Följande kodexempel visar hur du åsidosätter InitializeEditingControl metoden i en enkel klass som härleds från DataGridViewTextBoxCell klassen. Det här exemplet är en del av ett större kodexempel i How to: Host Controls in Windows Forms DataGridView Cells.

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

Kommentarer

Som en optimeringsteknik, vanligtvis alla celler av samma typ och i samma DataGridView dela en enda värdbaserad redigeringskontroll. Men innan kontrollen används av en cell måste den initieras med InitializeEditingControl metoden . Första gången den anropas lägger den här metoden till kontrollen i listan över redigeringskontroller i dess överordnade DataGridView. Den initierar också några av cellens visuella egenskaper. Anger till exempel InitializeEditingControl bakgrundsfärgen för redigeringsområdet så att den matchar den angivna cellformatparametern. Efterföljande anrop för att InitializeEditingControl inte göra någonting.

Härledda klasser använder den här metoden som värd för en instans av klassen Control som motsvarar deras typ. En tabell som innehåller ett eller flera DataGridViewTextBoxCell objekt anropar till exempel den här metoden för att lägga till en enda TextBox redigeringskontroll i ägandet DataGridView.

Gäller för

Se även