DataGridViewCell.InitializeEditingControl(Int32, Object, DataGridViewCellStyle) Método

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
Public Overridable Sub InitializeEditingControl (rowIndex As Integer, initialFormattedValue As Object, dataGridViewCellStyle As DataGridViewCellStyle)

Parámetros

rowIndex
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 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 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 método InitializeEditingControl en una clase simple que se deriva de la clase DataGridViewTextBoxCell.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 grande proporcionado en Cómo: hospedar controles en Windows Forms 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 técnica de optimización, normalmente todas las celdas del mismo tipo y en el mismo DataGridView compartir un único control de edición hospedado.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 una celda use el control, es necesario inicializarlo con el método InitializeEditingControl.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 controles de edición en su DataGridViewprincipal.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 hacen nada.Subsequent calls to InitializeEditingControl do nothing.

Las clases derivadas utilizan este método para hospedar una instancia de la clase Control correspondiente a su tipo.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 objetos DataGridViewTextBoxCell llama a este método para agregar un solo control de edición TextBox al DataGridViewpropietario.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: