DataGridViewCell.InitializeEditingControl 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化用於編輯儲存格的控制項。
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);
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
以零為基礎的列索引,代表該格子的位置。
- dataGridViewCellStyle
- DataGridViewCellStyle
代表細胞風格的A DataGridViewCellStyle 。
例外狀況
它沒有相關 DataGridView 聯的編輯控制,或者即使有,也沒有相關的編輯控制。
範例
以下程式碼範例示範如何在由該InitializeEditingControl類別衍生的簡單類別中覆寫該DataGridViewTextBoxCell方法。 此範例是《 如何在 Windows 表單 DataGridView 儲存格中操作:主機控制》中提供的完整程式碼範例的一部分。
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擁有 。