IDataGridViewEditingControl IDataGridViewEditingControl IDataGridViewEditingControl IDataGridViewEditingControl Interface

定義

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 Cells コントロールをホストします。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

注釈

このインターフェイスは実装によって、コントロールなどDataGridViewComboBoxEditingControlDataGridViewTextBoxEditingControl、対応するによってホストされるDataGridViewセルなど、DataGridViewComboBoxCellDataGridViewTextBoxCellが編集モードの場合。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.

セルの編集コントロールのセットをホストできる種類の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. DataGridViewコントロールがセルを呼び出してInitializeEditingControlメソッド。The DataGridView control calls the cell InitializeEditingControl method. 編集コントロールにセルの値を転送するには、このメソッドをオーバーライドすることができます。You can override this method to transfer the cell value to the editing control.

  3. DataGridViewコントロールが編集コントロールを呼び出すApplyCellStyleToEditingControlメソッドとセルの現在のスタイルに渡されます。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. DataGridViewコントロールが編集コントロールを呼び出すPrepareEditingControlForEditメソッド。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.

実装の詳細についてはIDataGridViewEditingControlを参照してください方法。Windows フォーム DataGridView Cells コントロールをホストします。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.

などの他のセル型DataGridViewButtonCellUI を提供するが、ユーザーが指定した値を格納しないでください。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 EditingControlDataGridView EditingControlDataGridView EditingControlDataGridView

セルを格納する DataGridView を取得または設定します。Gets or sets the DataGridView that contains the cell.

EditingControlFormattedValue EditingControlFormattedValue EditingControlFormattedValue EditingControlFormattedValue

エディターによって変更されるセルの書式設定された値を取得または設定します。Gets or sets the formatted value of the cell being modified by the editor.

EditingControlRowIndex EditingControlRowIndex EditingControlRowIndex EditingControlRowIndex

ホストしているセルの親行のインデックスを取得または設定します。Gets or sets the index of the hosting cell's parent row.

EditingControlValueChanged EditingControlValueChanged EditingControlValueChanged EditingControlValueChanged

編集コントロールの値と、そのコントロールをホストしているセルの値とが異なるかどうかを示す値を取得または設定します。Gets or sets a value indicating whether the value of the editing control differs from the value of the hosting cell.

EditingPanelCursor EditingPanelCursor EditingPanelCursor EditingPanelCursor

マウス ポインターが編集コントロールの上ではなく、EditingPanel の上にあるときに使用されるカーソルを取得します。Gets the cursor used when the mouse pointer is over the EditingPanel but not over the editing control.

RepositionEditingControlOnValueChange RepositionEditingControlOnValueChange RepositionEditingControlOnValueChange RepositionEditingControlOnValueChange

値が変更されるたびに、セルの内容の位置を変更する必要があるかどうかを示す値を取得または設定します。Gets or sets a value indicating whether the cell contents need to be repositioned whenever the value changes.

メソッド

ApplyCellStyleToEditingControl(DataGridViewCellStyle) ApplyCellStyleToEditingControl(DataGridViewCellStyle) ApplyCellStyleToEditingControl(DataGridViewCellStyle) ApplyCellStyleToEditingControl(DataGridViewCellStyle)

指定されたセル スタイルと矛盾しないように、コントロールのユーザー インターフェイス (UI) を変更します。Changes the control's user interface (UI) to be consistent with the specified cell style.

EditingControlWantsInputKey(Keys, Boolean) EditingControlWantsInputKey(Keys, Boolean) EditingControlWantsInputKey(Keys, Boolean) 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) GetEditingControlFormattedValue(DataGridViewDataErrorContexts) GetEditingControlFormattedValue(DataGridViewDataErrorContexts) GetEditingControlFormattedValue(DataGridViewDataErrorContexts)

セルの書式設定された値を取得します。Retrieves the formatted value of the cell.

PrepareEditingControlForEdit(Boolean) PrepareEditingControlForEdit(Boolean) PrepareEditingControlForEdit(Boolean) PrepareEditingControlForEdit(Boolean)

現在選択されているセルの編集を準備します。Prepares the currently selected cell for editing.

適用対象

こちらもご覧ください