Export (0) Print
Expand All

IDataGridViewEditingControl Interface

Defines common functionality for controls that are hosted within cells of a DataGridView.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

'Declaration
Public Interface IDataGridViewEditingControl

The IDataGridViewEditingControl type exposes the following members.

  NameDescription
Public propertyEditingControlDataGridViewGets or sets the DataGridView that contains the cell.
Public propertyEditingControlFormattedValueGets or sets the formatted value of the cell being modified by the editor.
Public propertyEditingControlRowIndexGets or sets the index of the hosting cell's parent row.
Public propertyEditingControlValueChangedGets or sets a value indicating whether the value of the editing control differs from the value of the hosting cell.
Public propertyEditingPanelCursorGets the cursor used when the mouse pointer is over the DataGridView.EditingPanel but not over the editing control.
Public propertyRepositionEditingControlOnValueChangeGets or sets a value indicating whether the cell contents need to be repositioned whenever the value changes.
Top

  NameDescription
Public methodApplyCellStyleToEditingControlChanges the control's user interface (UI) to be consistent with the specified cell style.
Public methodEditingControlWantsInputKeyDetermines whether the specified key is a regular input key that the editing control should process or a special key that the DataGridView should process.
Public methodGetEditingControlFormattedValueRetrieves the formatted value of the cell.
Public methodPrepareEditingControlForEditPrepares the currently selected cell for editing.
Top

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.

Cell types that can host editing controls set their EditType property to a Type representing the editing control type. When the cell enters edit mode, the following steps are performed:

  1. The DataGridView control creates an instance of the editing control type.

  2. The DataGridView control calls the cell InitializeEditingControl method. You can override this method to transfer the cell value to the editing control.

  3. The DataGridView control calls the editing control ApplyCellStyleToEditingControl method and passes in the cell's current style. You can implement this method to initialize the appearance of the editing control so that it matches the appearance of the cell.

  4. The DataGridView control calls the editing control PrepareEditingControlForEdit method. You can implement this method to make final adjustments to the editing control, such as selecting the control value.

For more information about implementing IDataGridViewEditingControl, see How to: Host Controls in Windows Forms DataGridView Cells.

Cell types such as DataGridViewCheckBoxCell that provide a user interface (UI) for specifying values without hosting an editing control implement the IDataGridViewEditingCell interface. The UI in this case is displayed regardless of whether the cell is in edit mode.

Other cell types, such as DataGridViewButtonCell, provide a UI but do not store user-specified values. In this case, the cell type does not implement IDataGridViewEditingCell or host an editing control.

The following code example provides an implementation of this interface that derives from DateTimePicker. This example is part of a larger example available in How to: Host Controls in Windows Forms DataGridView Cells.

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

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft