IDataGridViewEditingControl Interface

Définition

Définit les fonctionnalités courantes pour les contrôles hébergés dans les cellules d'un 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
Dérivé

Exemples

L’exemple de code suivant fournit une implémentation de cette interface qui dérive DateTimePickerde.The following code example provides an implementation of this interface that derives from DateTimePicker. Cet exemple fait partie d’un exemple plus complet disponible dans How to: Les contrôles hôtes dans Windows Forms lescellules 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

Remarques

Cette interface est implémentée par des contrôles, DataGridViewComboBoxEditingControl tels DataGridViewTextBoxEditingControlque et, qui sont hébergés par des cellules DataGridViewComboBoxCell correspondantes DataGridView , telles que et DataGridViewTextBoxCell, quand elles sont en mode édition.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.

Les types de cellules qui peuvent héberger des EditType contrôles d’édition Type définissent leur propriété sur un représentant le type de contrôle d’édition.Cell types that can host editing controls set their EditType property to a Type representing the editing control type. Lorsque la cellule passe en mode édition, les étapes suivantes sont effectuées:When the cell enters edit mode, the following steps are performed:

  1. Le DataGridView contrôle crée une instance du type de contrôle d’édition.The DataGridView control creates an instance of the editing control type.

  2. Le DataGridView contrôle appelle la méthode InitializeEditingControl de cellule.The DataGridView control calls the cell InitializeEditingControl method. Vous pouvez substituer cette méthode pour transférer la valeur de la cellule vers le contrôle d’édition.You can override this method to transfer the cell value to the editing control.

  3. Le DataGridView contrôle appelle la méthode de ApplyCellStyleToEditingControl contrôle d’édition et passe le style actuel de la cellule.The DataGridView control calls the editing control ApplyCellStyleToEditingControl method and passes in the cell's current style. Vous pouvez implémenter cette méthode pour initialiser l’apparence du contrôle d’édition afin qu’il corresponde à l’apparence de la cellule.You can implement this method to initialize the appearance of the editing control so that it matches the appearance of the cell.

  4. Le DataGridView contrôle appelle la méthode de PrepareEditingControlForEdit contrôle d’édition.The DataGridView control calls the editing control PrepareEditingControlForEdit method. Vous pouvez implémenter cette méthode pour effectuer les réglages finaux du contrôle d’édition, par exemple la sélection de la valeur du contrôle.You can implement this method to make final adjustments to the editing control, such as selecting the control value.

Pour plus d’informations sur IDataGridViewEditingControll’implémentation de, consultez Procédure: Les contrôles hôtes dans Windows Forms lescellules DataGridView.For more information about implementing IDataGridViewEditingControl, see How to: Host Controls in Windows Forms DataGridView Cells.

Les types de cellules DataGridViewCheckBoxCell tels que qui fournissent une interface utilisateur pour spécifier des valeurs sans héberger un contrôle d’édition IDataGridViewEditingCell implémentent l’interface.Cell types such as DataGridViewCheckBoxCell that provide a user interface (UI) for specifying values without hosting an editing control implement the IDataGridViewEditingCell interface. Dans ce cas, l’interface utilisateur s’affiche, que la cellule soit en mode édition ou non.The UI in this case is displayed regardless of whether the cell is in edit mode.

D’autres types de cellules, DataGridViewButtonCelltels que, fournissent une interface utilisateur, mais ne stockent pas de valeurs spécifiées par l’utilisateur.Other cell types, such as DataGridViewButtonCell, provide a UI but do not store user-specified values. Dans ce cas, le type de cellule n’implémente IDataGridViewEditingCell pas ou n’héberge pas de contrôle d’édition.In this case, the cell type does not implement IDataGridViewEditingCell or host an editing control.

Propriétés

EditingControlDataGridView

Obtient ou définit le DataGridView qui contient la cellule.Gets or sets the DataGridView that contains the cell.

EditingControlFormattedValue

Obtient ou définit la valeur mise en forme de la cellule en cours de modification par l'éditeur.Gets or sets the formatted value of the cell being modified by the editor.

EditingControlRowIndex

Obtient ou définit l'index de la ligne parente de la cellule d'hébergement.Gets or sets the index of the hosting cell's parent row.

EditingControlValueChanged

Obtient ou définit une valeur indiquant si la valeur du contrôle d'édition diffère de la valeur de la cellule d'hébergement.Gets or sets a value indicating whether the value of the editing control differs from the value of the hosting cell.

EditingPanelCursor

Obtient le curseur utilisé lorsque le pointeur de la souris se trouve sur EditingPanel mais pas sur le contrôle d'édition.Gets the cursor used when the mouse pointer is over the EditingPanel but not over the editing control.

RepositionEditingControlOnValueChange

Obtient ou définit une valeur indiquant si le contenu de la cellule doit être repositionné à chaque fois que la valeur change.Gets or sets a value indicating whether the cell contents need to be repositioned whenever the value changes.

Méthodes

ApplyCellStyleToEditingControl(DataGridViewCellStyle)

Modifie l'interface utilisateur du contrôle pour être qu'elle soit cohérente avec le style de cellule spécifié.Changes the control's user interface (UI) to be consistent with the specified cell style.

EditingControlWantsInputKey(Keys, Boolean)

Détermine si la touche spécifiée est une touche de saisie normale que le contrôle d'édition doit traiter ou une touche spéciale que le DataGridView doit traiter.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)

Récupère la valeur mise en forme de la cellule.Retrieves the formatted value of the cell.

PrepareEditingControlForEdit(Boolean)

Prépare la cellule actuellement sélectionnée pour la modification.Prepares the currently selected cell for editing.

S’applique à

Voir aussi