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 Forms 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

설명

이 인터페이스와 같은 컨트롤에 의해 구현 됩니다 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 Forms 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

셀이 들어 있는 DataGridView를 가져오거나 설정합니다.Gets 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.

적용 대상

추가 정보