DataGridViewCell.InitializeEditingControl(Int32, Object, DataGridViewCellStyle) DataGridViewCell.InitializeEditingControl(Int32, Object, DataGridViewCellStyle) DataGridViewCell.InitializeEditingControl(Int32, Object, DataGridViewCellStyle) Method

Definición

Inicializa el control utilizado para editar la celda.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);
abstract member InitializeEditingControl : int * obj * System.Windows.Forms.DataGridViewCellStyle -> unit
override this.InitializeEditingControl : int * obj * System.Windows.Forms.DataGridViewCellStyle -> unit

Parámetros

rowIndex
Int32 Int32 Int32

Índice de base cero de la fila de la ubicación de la celda.The zero-based row index of the cell's location.

initialFormattedValue
Object Object Object

Object que representa el valor que muestra la celda cuando se inicia la edición.An Object that represents the value displayed by the cell when editing is started.

dataGridViewCellStyle
DataGridViewCellStyle DataGridViewCellStyle DataGridViewCellStyle

DataGridViewCellStyle que representa el estilo de la celda.A DataGridViewCellStyle that represents the style of the cell.

Excepciones

No hay ningún control DataGridView asociado o, si alguno está presente, no tiene asociado un control de edición.There is no associated DataGridView or if one is present, it does not have an associated editing control.

Ejemplos

En el ejemplo de código siguiente se muestra cómo invalidar el InitializeEditingControl método en una clase simple que se deriva el DataGridViewTextBoxCell clase.The following code example demonstrates how to override the InitializeEditingControl method in a simple class that derives from the DataGridViewTextBoxCell class. Este ejemplo forma parte de un ejemplo de código más extenso en Cómo: Alojar controles en formularios de Windows las celdas de DataGridView.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

Comentarios

Como una técnica de optimización, normalmente todas las celdas del mismo tipo y en la misma DataGridView hospedado de recurso compartido de un único control de edición.As an optimization technique, typically all the cells of the same type and in the same DataGridView share a single hosted editing control. Sin embargo, antes de que el control se utiliza en una celda, debe inicializarse mediante la InitializeEditingControl método.However, before the control is used by a cell, it needs to be initialized by the InitializeEditingControl method. La primera vez que se llama, este método agrega el control a la lista de los controles en su elemento primario de edición DataGridView.The first time it is called, this method adds the control to the list of editing controls in its parent DataGridView. También inicializa algunas de las propiedades visuales de la celda.It also initializes some of the visual properties of the cell. Por ejemplo, InitializeEditingControl establece el color de fondo del área de edición para que coincida con el parámetro de estilo de celda proporcionado.For example, InitializeEditingControl sets the background color of the editing area to match the supplied cell style parameter. Las llamadas subsiguientes a InitializeEditingControl no hacer nada.Subsequent calls to InitializeEditingControl do nothing.

Las clases derivadas utilizan este método para hospedar una instancia de la Control corresponde a su tipo de clase.Derived classes use this method to host an instance of the Control class corresponding to their type. Por ejemplo, una tabla que contiene uno o varios DataGridViewTextBoxCell objetos llama a este método para agregar una sola TextBox control propietario de edición DataGridView.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.

Se aplica a

Consulte también: