共用方式為


IDataGridViewEditingControl 介面

定義

為裝載於 DataGridView 之儲存格的控制項定義通用功能。

public interface class IDataGridViewEditingControl
public interface IDataGridViewEditingControl
type IDataGridViewEditingControl = interface
Public Interface IDataGridViewEditingControl
衍生

範例

下列程式碼範例提供衍生自 DateTimePicker 的這個介面實作。 此範例是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

備註

當這些儲存格處於編輯模式時,這個介面是由對應 DataGridView 儲存格所裝載的控制項, DataGridViewTextBoxEditingControlDataGridViewComboBoxEditingControl 例如 DataGridViewComboBoxCellDataGridViewTextBoxCell 所實作。

可以裝載編輯控制項的儲存格類型,將其 EditType 屬性設定為 Type 代表編輯控制項類型的 。 當儲存格進入編輯模式時,會執行下列步驟:

  1. 控制項 DataGridView 會建立編輯控制項類型的實例。

  2. 控制項 DataGridView 會呼叫儲存格 InitializeEditingControl 方法。 您可以覆寫這個方法,將儲存格值傳送至編輯控制項。

  3. 控制項 DataGridView 會呼叫編輯控制項 ApplyCellStyleToEditingControl 方法,並傳入儲存格的目前樣式。 您可以實作此方法來初始化編輯控制項的外觀,使其符合儲存格的外觀。

  4. 控制項 DataGridView 會呼叫編輯控制項 PrepareEditingControlForEdit 方法。 您可以實作這個方法,以對編輯控制項進行最終調整,例如選取控制項值。

如需實作 IDataGridViewEditingControl 的詳細資訊,請參閱How to: Host Controls in Windows Forms DataGridView Cells

資料格類型,例如 DataGridViewCheckBoxCell ,提供使用者介面 (UI) 來指定值,而不需要裝載編輯控制項實作 IDataGridViewEditingCell 介面。 不論儲存格是否處於編輯模式,此案例中的 UI 都會顯示。

其他儲存格類型,例如 DataGridViewButtonCell ,會提供 UI,但不會儲存使用者指定的值。 在此情況下,儲存格類型不會實 IDataGridViewEditingCell 作或裝載編輯控制項。

屬性

EditingControlDataGridView

取得或設定包含儲存格的 DataGridView

EditingControlFormattedValue

取得或設定編輯器所修改之儲存格的格式化值。

EditingControlRowIndex

取得或設定裝載儲存格之父資料列的索引。

EditingControlValueChanged

取得或設定值,指出編輯控制項的值是否與裝載儲存格的值不同。

EditingPanelCursor

取得當滑鼠指標放在 EditingPanel 上方,但不在編輯控制項上方時所用的游標。

RepositionEditingControlOnValueChange

取得或設定值,指出每當值變更時儲存格內容是否需要重新調整位置。

方法

ApplyCellStyleToEditingControl(DataGridViewCellStyle)

變更控制項的使用者介面 (UI),使其與指定的儲存格樣式一致。

EditingControlWantsInputKey(Keys, Boolean)

判斷指定的按鍵是否為編輯控制項應該處理的標準輸入按鍵,或是 DataGridView 應該處理的特殊按鍵。

GetEditingControlFormattedValue(DataGridViewDataErrorContexts)

擷取儲存格的格式化值。

PrepareEditingControlForEdit(Boolean)

準備目前所選的儲存格來編輯。

適用於

另請參閱