DataGridViewCell.InitializeEditingControl(Int32, Object, DataGridViewCellStyle) DataGridViewCell.InitializeEditingControl(Int32, Object, DataGridViewCellStyle) DataGridViewCell.InitializeEditingControl(Int32, Object, DataGridViewCellStyle) Method

定義

初始化用來編輯儲存格的控制項。Initializes the control used to edit the cell.

public:
 virtual void InitializeEditingControl(int rowIndex, System::Object ^ initialFormattedValue, System::Windows::Forms::DataGridViewCellStyle ^ dataGridViewCellStyle);
public virtual void InitializeEditingControl (int rowIndex, object initialFormattedValue, System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle);
abstract member InitializeEditingControl : int * obj * System.Windows.Forms.DataGridViewCellStyle -> unit
override this.InitializeEditingControl : int * obj * System.Windows.Forms.DataGridViewCellStyle -> unit

參數

rowIndex
Int32 Int32 Int32

儲存格位置之以零起始的資料列索引。The zero-based row index of the cell's location.

initialFormattedValue
Object Object Object

Object,表示在開始編輯時,由儲存格顯示的值。An Object that represents the value displayed by the cell when editing is started.

dataGridViewCellStyle
DataGridViewCellStyle DataGridViewCellStyle DataGridViewCellStyle

DataGridViewCellStyle,表示儲存格的樣式。A DataGridViewCellStyle that represents the style of the cell.

例外狀況

沒有相關聯的 DataGridView,或者如果有一個相關聯的檢視表,它並沒有相關聯的編輯控制項。There is no associated DataGridView or if one is present, it does not have an associated editing control.

範例

下列程式碼範例示範如何在衍生InitializeEditingControl DataGridViewTextBoxCell自類別的簡單類別中覆寫方法。The following code example demonstrates how to override the InitializeEditingControl method in a simple class that derives from the DataGridViewTextBoxCell class. 這個範例是在如何:中提供的較大程式碼範例的一部分。Windows Forms DataGridView 儲存格中的主控制項。This example is part of a larger code example provided in How to: Host Controls in Windows Forms DataGridView Cells.

public class CalendarCell : DataGridViewTextBoxCell
{

    public CalendarCell()
        : base()
    {
        // Use the short date format.
        this.Style.Format = "d";
    }

    public override void InitializeEditingControl(int rowIndex, object 
        initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
    {
        // Set the value of the editing control to the current cell value.
        base.InitializeEditingControl(rowIndex, initialFormattedValue, 
            dataGridViewCellStyle);
        CalendarEditingControl ctl = 
            DataGridView.EditingControl as CalendarEditingControl;
        // Use the default row value when Value property is null.
        if (this.Value == null)
        {
            ctl.Value = (DateTime)this.DefaultNewRowValue;
        }
        else
        {
            ctl.Value = (DateTime)this.Value;
        }
    }

    public override Type EditType
    {
        get
        {
            // Return the type of the editing control that CalendarCell uses.
            return typeof(CalendarEditingControl);
        }
    }

    public override Type ValueType
    {
        get
        {
            // Return the type of the value that CalendarCell contains.
            
            return typeof(DateTime);
        }
    }

    public override object DefaultNewRowValue
    {
        get
        {
            // Use the current date and time as the default value.
            return DateTime.Now;
        }
    }
}
Public Class CalendarCell
    Inherits DataGridViewTextBoxCell

    Public Sub New()
        ' Use the short date format.
        Me.Style.Format = "d"
    End Sub

    Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _
        ByVal initialFormattedValue As Object, _
        ByVal dataGridViewCellStyle As DataGridViewCellStyle)

        ' Set the value of the editing control to the current cell value.
        MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _
            dataGridViewCellStyle)

        Dim ctl As CalendarEditingControl = _
            CType(DataGridView.EditingControl, CalendarEditingControl)

        ' Use the default row value when Value property is null.
        If (Me.Value Is Nothing) Then
            ctl.Value = CType(Me.DefaultNewRowValue, DateTime)
        Else
            ctl.Value = CType(Me.Value, DateTime)
        End If
    End Sub

    Public Overrides ReadOnly Property EditType() As Type
        Get
            ' Return the type of the editing control that CalendarCell uses.
            Return GetType(CalendarEditingControl)
        End Get
    End Property

    Public Overrides ReadOnly Property ValueType() As Type
        Get
            ' Return the type of the value that CalendarCell contains.
            Return GetType(DateTime)
        End Get
    End Property

    Public Overrides ReadOnly Property DefaultNewRowValue() As Object
        Get
            ' Use the current date and time as the default value.
            Return DateTime.Now
        End Get
    End Property

End Class

備註

做為一種優化技巧,通常所有相同類型和相同DataGridView的資料格都是單一裝載編輯控制項。As an optimization technique, typically all the cells of the same type and in the same DataGridView share a single hosted editing control. 不過,在資料格使用控制項之前,它必須由InitializeEditingControl方法初始化。However, before the control is used by a cell, it needs to be initialized by the InitializeEditingControl method. 第一次呼叫它時,這個方法會將控制項加入其父系DataGridView的編輯控制項清單中。The first time it is called, this method adds the control to the list of editing controls in its parent DataGridView. 它也會初始化儲存格的部分視覺屬性。It also initializes some of the visual properties of the cell. 例如, InitializeEditingControl設定編輯區域的背景色彩,使其符合提供的儲存格樣式參數。For example, InitializeEditingControl sets the background color of the editing area to match the supplied cell style parameter. 後續呼叫InitializeEditingControl不會執行任何動作。Subsequent calls to InitializeEditingControl do nothing.

衍生類別會使用這個方法來裝載對應至其Control型別之類別的實例。Derived classes use this method to host an instance of the Control class corresponding to their type. 例如,包含一或多個DataGridViewTextBoxCell物件的資料表會呼叫這個方法,將單一TextBox編輯控制項加入至擁有DataGridViewFor example, a table that contains one or more DataGridViewTextBoxCell objects calls this method to add a single TextBox editing control to the owning DataGridView.

適用於

另請參閱