Επεξεργασία

Κοινή χρήση μέσω


DataGridViewCell.InitializeEditingControl Method

Definition

Initializes the control used to edit the cell.

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)

Parameters

rowIndex
Int32

The zero-based row index of the cell's location.

initialFormattedValue
Object

An Object that represents the value displayed by the cell when editing is started.

dataGridViewCellStyle
DataGridViewCellStyle

A DataGridViewCellStyle that represents the style of the cell.

Exceptions

There is no associated DataGridView or if one is present, it does not have an associated editing control.

Examples

The following code example demonstrates how to override the InitializeEditingControl method in a simple class that derives from the DataGridViewTextBoxCell class. This example is part of a larger code example provided in 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

Remarks

As an optimization technique, typically all the cells of the same type and in the same DataGridView share a single hosted editing control. However, before the control is used by a cell, it needs to be initialized by the InitializeEditingControl method. The first time it is called, this method adds the control to the list of editing controls in its parent DataGridView. It also initializes some of the visual properties of the cell. For example, InitializeEditingControl sets the background color of the editing area to match the supplied cell style parameter. Subsequent calls to InitializeEditingControl do nothing.

Derived classes use this method to host an instance of the Control class corresponding to their type. For example, a table that contains one or more DataGridViewTextBoxCell objects calls this method to add a single TextBox editing control to the owning DataGridView.

Applies to

See also