Export (0) Print
Expand All
Expand Minimize

DataGridViewCell.InitializeEditingControl Method

Initializes the control used to edit the cell.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public virtual void InitializeEditingControl(
	int rowIndex,
	Object initialFormattedValue,
	DataGridViewCellStyle dataGridViewCellStyle
)

Parameters

rowIndex
Type: System.Int32

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

initialFormattedValue
Type: System.Object

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

dataGridViewCellStyle
Type: System.Windows.Forms.DataGridViewCellStyle

A DataGridViewCellStyle that represents the style of the cell.

ExceptionCondition
InvalidOperationException

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

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.

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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft