IDataGridViewEditingControl Interfaz

Definición

Define una funcionalidad común para los controles que se hospedan en las celdas de un control DataGridView.Defines common functionality for controls that are hosted within cells of a DataGridView.

public interface class IDataGridViewEditingControl
public interface IDataGridViewEditingControl
type IDataGridViewEditingControl = interface
Public Interface IDataGridViewEditingControl
Derivado

Ejemplos

En el ejemplo de código siguiente se proporciona una implementación de esta interfaz que DateTimePickerderiva de.The following code example provides an implementation of this interface that derives from DateTimePicker. Este ejemplo forma parte de un ejemplo más grande disponible en cómo: Controles host en Windows Forms celdasde DataGridView.This example is part of a larger example available in How to: Host Controls in Windows Forms DataGridView Cells.

class CalendarEditingControl : DateTimePicker, IDataGridViewEditingControl
{
    DataGridView dataGridView;
    private bool valueChanged = false;
    int rowIndex;

    public CalendarEditingControl()
    {
        this.Format = DateTimePickerFormat.Short;
    }

    // Implements the IDataGridViewEditingControl.EditingControlFormattedValue 
    // property.
    public object EditingControlFormattedValue
    {
        get
        {
            return this.Value.ToShortDateString();
        }
        set
        {            
            if (value is String)
            {
                try
                {
                    // This will throw an exception of the string is 
                    // null, empty, or not in the format of a date.
                    this.Value = DateTime.Parse((String)value);
                }
                catch
                {
                    // In the case of an exception, just use the 
                    // default value so we're not left with a null
                    // value.
                    this.Value = DateTime.Now;
                }
            }
        }
    }

    // Implements the 
    // IDataGridViewEditingControl.GetEditingControlFormattedValue method.
    public object GetEditingControlFormattedValue(
        DataGridViewDataErrorContexts context)
    {
        return EditingControlFormattedValue;
    }

    // Implements the 
    // IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
    public void ApplyCellStyleToEditingControl(
        DataGridViewCellStyle dataGridViewCellStyle)
    {
        this.Font = dataGridViewCellStyle.Font;
        this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
        this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
    }

    // Implements the IDataGridViewEditingControl.EditingControlRowIndex 
    // property.
    public int EditingControlRowIndex
    {
        get
        {
            return rowIndex;
        }
        set
        {
            rowIndex = value;
        }
    }

    // Implements the IDataGridViewEditingControl.EditingControlWantsInputKey 
    // method.
    public bool EditingControlWantsInputKey(
        Keys key, bool dataGridViewWantsInputKey)
    {
        // Let the DateTimePicker handle the keys listed.
        switch (key & Keys.KeyCode)
        {
            case Keys.Left:
            case Keys.Up:
            case Keys.Down:
            case Keys.Right:
            case Keys.Home:
            case Keys.End:
            case Keys.PageDown:
            case Keys.PageUp:
                return true;
            default:
                return !dataGridViewWantsInputKey;
        }
    }

    // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit 
    // method.
    public void PrepareEditingControlForEdit(bool selectAll)
    {
        // No preparation needs to be done.
    }

    // Implements the IDataGridViewEditingControl
    // .RepositionEditingControlOnValueChange property.
    public bool RepositionEditingControlOnValueChange
    {
        get
        {
            return false;
        }
    }

    // Implements the IDataGridViewEditingControl
    // .EditingControlDataGridView property.
    public DataGridView EditingControlDataGridView
    {
        get
        {
            return dataGridView;
        }
        set
        {
            dataGridView = value;
        }
    }

    // Implements the IDataGridViewEditingControl
    // .EditingControlValueChanged property.
    public bool EditingControlValueChanged
    {
        get
        {
            return valueChanged;
        }
        set
        {
            valueChanged = value;
        }
    }

    // Implements the IDataGridViewEditingControl
    // .EditingPanelCursor property.
    public Cursor EditingPanelCursor
    {
        get
        {
            return base.Cursor;
        }
    }

    protected override void OnValueChanged(EventArgs eventargs)
    {
        // Notify the DataGridView that the contents of the cell
        // have changed.
        valueChanged = true;
        this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
        base.OnValueChanged(eventargs);
    }
}
Class CalendarEditingControl
    Inherits DateTimePicker
    Implements IDataGridViewEditingControl

    Private dataGridViewControl As DataGridView
    Private valueIsChanged As Boolean = False
    Private rowIndexNum As Integer

    Public Sub New()
        Me.Format = DateTimePickerFormat.Short
    End Sub

    Public Property EditingControlFormattedValue() As Object _
        Implements IDataGridViewEditingControl.EditingControlFormattedValue

        Get
            Return Me.Value.ToShortDateString()
        End Get

        Set(ByVal value As Object)
            Try
                ' This will throw an exception of the string is 
                ' null, empty, or not in the format of a date.
                Me.Value = DateTime.Parse(CStr(value))
            Catch
                ' In the case of an exception, just use the default
                ' value so we're not left with a null value.
                Me.Value = DateTime.Now
            End Try
        End Set

    End Property

    Public Function GetEditingControlFormattedValue(ByVal context _
        As DataGridViewDataErrorContexts) As Object _
        Implements IDataGridViewEditingControl.GetEditingControlFormattedValue

        Return Me.Value.ToShortDateString()

    End Function

    Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As _
        DataGridViewCellStyle) _
        Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl

        Me.Font = dataGridViewCellStyle.Font
        Me.CalendarForeColor = dataGridViewCellStyle.ForeColor
        Me.CalendarMonthBackground = dataGridViewCellStyle.BackColor

    End Sub

    Public Property EditingControlRowIndex() As Integer _
        Implements IDataGridViewEditingControl.EditingControlRowIndex

        Get
            Return rowIndexNum
        End Get
        Set(ByVal value As Integer)
            rowIndexNum = value
        End Set

    End Property

    Public Function EditingControlWantsInputKey(ByVal key As Keys, _
        ByVal dataGridViewWantsInputKey As Boolean) As Boolean _
        Implements IDataGridViewEditingControl.EditingControlWantsInputKey

        ' Let the DateTimePicker handle the keys listed.
        Select Case key And Keys.KeyCode
            Case Keys.Left, Keys.Up, Keys.Down, Keys.Right, _
                Keys.Home, Keys.End, Keys.PageDown, Keys.PageUp

                Return True

            Case Else
                Return Not dataGridViewWantsInputKey
        End Select

    End Function

    Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) _
        Implements IDataGridViewEditingControl.PrepareEditingControlForEdit

        ' No preparation needs to be done.

    End Sub

    Public ReadOnly Property RepositionEditingControlOnValueChange() _
        As Boolean Implements _
        IDataGridViewEditingControl.RepositionEditingControlOnValueChange

        Get
            Return False
        End Get

    End Property

    Public Property EditingControlDataGridView() As DataGridView _
        Implements IDataGridViewEditingControl.EditingControlDataGridView

        Get
            Return dataGridViewControl
        End Get
        Set(ByVal value As DataGridView)
            dataGridViewControl = value
        End Set

    End Property

    Public Property EditingControlValueChanged() As Boolean _
        Implements IDataGridViewEditingControl.EditingControlValueChanged

        Get
            Return valueIsChanged
        End Get
        Set(ByVal value As Boolean)
            valueIsChanged = value
        End Set

    End Property

    Public ReadOnly Property EditingControlCursor() As Cursor _
        Implements IDataGridViewEditingControl.EditingPanelCursor

        Get
            Return MyBase.Cursor
        End Get

    End Property

    Protected Overrides Sub OnValueChanged(ByVal eventargs As EventArgs)

        ' Notify the DataGridView that the contents of the cell have changed.
        valueIsChanged = True
        Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
        MyBase.OnValueChanged(eventargs)

    End Sub

End Class

Comentarios

Esta interfaz se DataGridViewComboBoxEditingControl implementa mediante controles, como y DataGridViewTextBoxEditingControl, que se hospedan en las DataGridView celdas DataGridViewComboBoxCell correspondientes, como y DataGridViewTextBoxCell, cuando están en modo de edición.This interface is implemented by controls, such as DataGridViewComboBoxEditingControl and DataGridViewTextBoxEditingControl, that are hosted by corresponding DataGridView cells, such as DataGridViewComboBoxCell and DataGridViewTextBoxCell, when they are in edit mode.

Los tipos de celdas que pueden hospedar controles EditType de edición establecen Type su propiedad en que representa el tipo de control de edición.Cell types that can host editing controls set their EditType property to a Type representing the editing control type. Cuando la celda entra en el modo de edición, se llevan a cabo los siguientes pasos:When the cell enters edit mode, the following steps are performed:

  1. El DataGridView control crea una instancia del tipo de control de edición.The DataGridView control creates an instance of the editing control type.

  2. El DataGridView control llama al método InitializeEditingControl de la celda.The DataGridView control calls the cell InitializeEditingControl method. Puede invalidar este método para transferir el valor de la celda al control de edición.You can override this method to transfer the cell value to the editing control.

  3. El DataGridView control llama al método de ApplyCellStyleToEditingControl control de edición y pasa el estilo actual de la celda.The DataGridView control calls the editing control ApplyCellStyleToEditingControl method and passes in the cell's current style. Puede implementar este método para inicializar el aspecto del control de edición de forma que coincida con el aspecto de la celda.You can implement this method to initialize the appearance of the editing control so that it matches the appearance of the cell.

  4. El DataGridView control llama al método de PrepareEditingControlForEdit control de edición.The DataGridView control calls the editing control PrepareEditingControlForEdit method. Puede implementar este método para realizar ajustes finales en el control de edición, como seleccionar el valor del control.You can implement this method to make final adjustments to the editing control, such as selecting the control value.

Para obtener más información acerca IDataGridViewEditingControlde la implementación de, consulte Cómo: Controles host en Windows Forms celdasde DataGridView.For more information about implementing IDataGridViewEditingControl, see How to: Host Controls in Windows Forms DataGridView Cells.

Los tipos de celda DataGridViewCheckBoxCell como que proporcionan una interfaz de usuario (UI) para especificar valores sin hospedar un control de IDataGridViewEditingCell edición implementan la interfaz.Cell types such as DataGridViewCheckBoxCell that provide a user interface (UI) for specifying values without hosting an editing control implement the IDataGridViewEditingCell interface. En este caso, la interfaz de usuario se muestra independientemente de si la celda está en modo de edición.The UI in this case is displayed regardless of whether the cell is in edit mode.

Otros tipos de celda, como DataGridViewButtonCell, proporcionan una interfaz de usuario, pero no almacenan los valores especificados por el usuario.Other cell types, such as DataGridViewButtonCell, provide a UI but do not store user-specified values. En este caso, el tipo de celda no implementa IDataGridViewEditingCell ni hospeda un control de edición.In this case, the cell type does not implement IDataGridViewEditingCell or host an editing control.

Propiedades

EditingControlDataGridView

Obtiene o establece el control DataGridView que contiene la celda.Gets or sets the DataGridView that contains the cell.

EditingControlFormattedValue

Obtiene o establece el valor con formato de la celda que el editor está modificando.Gets or sets the formatted value of the cell being modified by the editor.

EditingControlRowIndex

Obtiene o establece el índice correspondiente a la fila primaria de la celda en la que se hospeda.Gets or sets the index of the hosting cell's parent row.

EditingControlValueChanged

Obtiene o establece un valor que indica si el valor del control de edición difiere del valor de la celda donde se hospeda.Gets or sets a value indicating whether the value of the editing control differs from the value of the hosting cell.

EditingPanelCursor

Obtiene el cursor que se utiliza cuando el puntero del mouse se sitúa sobre la propiedad EditingPanel, pero no sobre el control de edición.Gets the cursor used when the mouse pointer is over the EditingPanel but not over the editing control.

RepositionEditingControlOnValueChange

Obtiene o establece un valor que indica si el contenido de la celda debe cambiar de posición cada vez que se modifique el valor.Gets or sets a value indicating whether the cell contents need to be repositioned whenever the value changes.

Métodos

ApplyCellStyleToEditingControl(DataGridViewCellStyle)

Cambia la interfaz de usuario (UI) del control para que sea coherente con el estilo de celda especificado.Changes the control's user interface (UI) to be consistent with the specified cell style.

EditingControlWantsInputKey(Keys, Boolean)

Determina si la tecla especificada es una tecla de entrada estándar que el control de edición debería procesar, o una clave especial que DataGridView debe procesar.Determines whether the specified key is a regular input key that the editing control should process or a special key that the DataGridView should process.

GetEditingControlFormattedValue(DataGridViewDataErrorContexts)

Recupera el valor con formato de la celda.Retrieves the formatted value of the cell.

PrepareEditingControlForEdit(Boolean)

Prepara la celda actualmente seleccionada para su edición.Prepares the currently selected cell for editing.

Se aplica a

Consulte también: