IDataGridViewEditingControl 接口

定义

定义承载在 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
派生

示例

下面的代码示例提供了一个派生自DateTimePicker的接口的实现。The following code example provides an implementation of this interface that derives from DateTimePicker. 此示例摘自了如何:Windows 窗体 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

注解

DataGridViewComboBoxEditingControl接口由DataGridView DataGridViewTextBoxEditingControl相应的控件(DataGridViewTextBoxCell如和) 实现, 这些控件在处于编辑模式时由相应的单元 (例如和)承载。DataGridViewComboBoxCellThis 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.

可以承载编辑控件的单元格类型将EditType其属性设置Type为, 表示编辑控件类型。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. DataGridView控件创建编辑控件类型的实例。The DataGridView control creates an instance of the editing control type.

  2. 控件调用单元InitializeEditingControl方法。 DataGridViewThe DataGridView control calls the cell InitializeEditingControl method. 您可以重写此方法以将单元值传输到编辑控件。You can override this method to transfer the cell value to the editing control.

  3. 控件调用编辑控件ApplyCellStyleToEditingControl方法并传入单元格的当前样式。 DataGridViewThe 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. 控件调用编辑控件PrepareEditingControlForEdit方法。 DataGridViewThe 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.

有关实现IDataGridViewEditingControl的详细信息, 请参阅如何:Windows 窗体 DataGridView 单元中的主机控件。For more information about implementing IDataGridViewEditingControl, see How to: Host Controls in Windows Forms DataGridView Cells.

诸如DataGridViewCheckBoxCell这样的单元格类型提供用户界面 (UI) 来指定值, 而无需承载编辑控件IDataGridViewEditingCell即可实现接口。Cell types such as DataGridViewCheckBoxCell that provide a user interface (UI) for specifying values without hosting an editing control implement the IDataGridViewEditingCell interface. 如果该单元格处于编辑模式, 则显示此示例中的 UI。The UI in this case is displayed regardless of whether the cell is in edit mode.

其他单元类型 (如DataGridViewButtonCell) 提供 UI, 但不存储用户指定的值。Other cell types, such as DataGridViewButtonCell, provide a UI but do not store user-specified values. 在这种情况下, 单元类型不实现IDataGridViewEditingCell或承载编辑控件。In this case, the cell type does not implement IDataGridViewEditingCell or host an editing control.

属性

EditingControlDataGridView

获取或设置包含单元格的 DataGridViewGets or sets the DataGridView that contains the cell.

EditingControlFormattedValue

获取或设置编辑器正在修改的单元格的格式化值。Gets or sets the formatted value of the cell being modified by the editor.

EditingControlRowIndex

获取或设置该承载单元格的父行的索引。Gets or sets the index of the hosting cell's parent row.

EditingControlValueChanged

获取或设置一个值,该值指示编辑控件的值是否与承载单元格的值不同。Gets or sets a value indicating whether the value of the editing control differs from the value of the hosting cell.

EditingPanelCursor

获取当鼠标指针位于 EditingPanel 上方但不位于编辑控件上方时所使用的光标。Gets the cursor used when the mouse pointer is over the EditingPanel but not over the editing control.

RepositionEditingControlOnValueChange

获取或设置一个值,该值指示每当值更改时,是否需要重新定位单元格的内容。Gets or sets a value indicating whether the cell contents need to be repositioned whenever the value changes.

方法

ApplyCellStyleToEditingControl(DataGridViewCellStyle)

更改控件的用户界面 (UI),使之与指定单元格样式一致。Changes the control's user interface (UI) to be consistent with the specified cell style.

EditingControlWantsInputKey(Keys, Boolean)

确定指定的键是应由编辑控件处理的常规输入键,还是应由 DataGridView 处理的特殊键。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)

检索单元格的格式化值。Retrieves the formatted value of the cell.

PrepareEditingControlForEdit(Boolean)

准备当前选中的单元格以进行编辑。Prepares the currently selected cell for editing.

适用于

另请参阅