InitializeEditingControl Method
Collapse the table of content
Expand the table of content

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


Type: System.Int32
The zero-based row index of the cell's location.
Type: System.Object
An Object that represents the value displayed by the cell when editing is started.
Type: System.Windows.Forms.DataGridViewCellStyle
A DataGridViewCellStyle that represents the style of the cell.


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, 
        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;
            ctl.Value = (DateTime)this.Value;

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

    public override Type ValueType
            // Return the type of the value that CalendarCell contains.

            return typeof(DateTime);

    public override object DefaultNewRowValue
            // Use the current date and time as the default value.
            return DateTime.Now;

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

Community Additions

© 2016 Microsoft