DataGridViewCell.InitializeEditingControl 方法

定義

初始化用來編輯儲存格的控制項。

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
Public Overridable Sub InitializeEditingControl (rowIndex As Integer, initialFormattedValue As Object, dataGridViewCellStyle As DataGridViewCellStyle)

參數

rowIndex
Int32

儲存格位置之以零為起始的資料列索引。

initialFormattedValue
Object

Object,表示在開始編輯時,由儲存格顯示的值。

dataGridViewCellStyle
DataGridViewCellStyle

代表儲存格樣式的 DataGridViewCellStyle

例外狀況

沒有相關聯的 DataGridView,或者如果有一個相關聯的檢視表,它並沒有相關聯的編輯控制項。

範例

下列程式碼範例示範如何在衍生自 DataGridViewTextBoxCell 類別的簡單類別中覆寫 InitializeEditingControl 方法。 此範例是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 共用單一裝載的編輯控制項。 不過,在儲存格使用控制項之前,它必須先由 InitializeEditingControl 方法初始化。 第一次呼叫它時,這個方法會將控制項新增至其父 DataGridView 代 中的編輯控制項清單。 它也會初始化儲存格的一些視覺屬性。 例如, InitializeEditingControl 設定編輯區域的背景色彩,以符合提供的儲存格樣式參數。 後續呼叫不 InitializeEditingControl 執行任何動作。

衍生類別會使用這個方法來裝載對應至其型別之 Control 類別的實例。 例如,包含一或多個 DataGridViewTextBoxCell 物件的資料表會呼叫這個方法,將單 TextBox 一編輯控制項加入至擁有 DataGridView 的 。

適用於

另請參閱