IDataGridViewEditingControl Rozhraní

Definice

Definuje běžné funkce ovládacích prvků hostovaných v buňkách DataGridView.

public interface class IDataGridViewEditingControl
public interface IDataGridViewEditingControl
type IDataGridViewEditingControl = interface
Public Interface IDataGridViewEditingControl
Odvozené

Příklady

Následující příklad kódu poskytuje implementaci tohoto rozhraní, které je odvozeno od DateTimePicker. Tento příklad je součástí rozsáhlejšího příkladu, který je k dispozici v části Postupy: Hostitelské ovládací prvky v buňkách model Windows Forms DataGridView.

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

Poznámky

Toto rozhraní je implementováno ovládacími prvky, jako DataGridViewComboBoxEditingControl jsou a DataGridViewTextBoxEditingControl, které jsou hostovány odpovídajícími DataGridView buňkami, například DataGridViewComboBoxCell a DataGridViewTextBoxCell, když jsou v režimu úprav.

Typy buněk, které mohou hostovat ovládací prvky pro úpravy, nastaví jejich EditType vlastnost na Type představující typ ovládacího prvku pro úpravy. Když buňka přejde do režimu úprav, provede se následující kroky:

  1. Ovládací DataGridView prvek vytvoří instanci typu ovládacího prvku pro úpravy.

  2. Ovládací DataGridView prvek volá metodu buňky InitializeEditingControl . Tuto metodu můžete přepsat tak, aby se hodnota buňky přenesla do ovládacího prvku pro úpravy.

  3. Ovládací DataGridView prvek volá metodu ovládacího prvku ApplyCellStyleToEditingControl pro úpravy a předává aktuální styl buňky. Tuto metodu můžete implementovat k inicializaci vzhledu ovládacího prvku pro úpravy tak, aby odpovídal vzhledu buňky.

  4. Ovládací DataGridView prvek volá metodu ovládacího prvku PrepareEditingControlForEdit pro úpravy. Tuto metodu můžete implementovat, abyste provedli konečné úpravy ovládacího prvku pro úpravy, jako je například výběr hodnoty ovládacího prvku.

Další informace o implementaci IDataGridViewEditingControlnaleznete v tématu Postupy: Host Controls in model Windows Forms DataGridView Cells.

Typy buněk, například DataGridViewCheckBoxCell které poskytují uživatelské rozhraní pro zadávání hodnot bez hostování ovládacího prvku pro úpravy, implementují IDataGridViewEditingCell rozhraní. Uživatelské rozhraní se v tomto případě zobrazí bez ohledu na to, jestli je buňka v režimu úprav.

Jiné typy buněk, například DataGridViewButtonCell, poskytují uživatelské rozhraní, ale neukládají uživatelem zadané hodnoty. V tomto případě typ buňky neimplementuje IDataGridViewEditingCell nebo hostuje ovládací prvek pro úpravy.

Vlastnosti

EditingControlDataGridView

Získá nebo nastaví DataGridView , který obsahuje buňku.

EditingControlFormattedValue

Získá nebo nastaví formátovanou hodnotu buňky, kterou upravuje editor.

EditingControlRowIndex

Získá nebo nastaví index nadřazeného řádku hostitelské buňky.

EditingControlValueChanged

Získá nebo nastaví hodnotu určující, zda hodnota ovládacího prvku pro úpravy se liší od hodnoty hostitelské buňky.

EditingPanelCursor

Získá kurzor použitý, když je ukazatel myši nad EditingPanel ovládacím prvku pro úpravy, ale ne přes ovládací prvek pro úpravy.

RepositionEditingControlOnValueChange

Získá nebo nastaví hodnotu označující, zda se obsah buňky musí přemístit při každé změně hodnoty.

Metody

ApplyCellStyleToEditingControl(DataGridViewCellStyle)

Změní uživatelské rozhraní ovládacího prvku tak, aby odpovídalo zadanému stylu buňky.

EditingControlWantsInputKey(Keys, Boolean)

Určuje, zda je zadaný klíč běžným vstupním klíčem, který má ovládací prvek pro úpravy zpracovat, nebo speciální klíč, který DataGridView by měl zpracovat.

GetEditingControlFormattedValue(DataGridViewDataErrorContexts)

Načte formátovanou hodnotu buňky.

PrepareEditingControlForEdit(Boolean)

Připraví aktuálně vybranou buňku pro úpravy.

Platí pro

Viz také