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);
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
セルの位置の 0 から始まる行インデックス。
- dataGridViewCellStyle
- DataGridViewCellStyle
セルのスタイルを表す DataGridViewCellStyle 。
例外
関連付けられた DataGridView が存在しないか、存在する場合は、関連付けられた編集コントロールがありません。
例
次のコード例では、DataGridViewTextBoxCell クラスから派生する単純なクラスのInitializeEditingControl メソッドをオーバーライドする方法を示します。 この例は、「 方法: 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 のすべてのセルは、1 つのホストされた編集コントロールを共有します。 ただし、コントロールをセルで使用する前に、 InitializeEditingControl メソッドで初期化する必要があります。 このメソッドは、初めて呼び出されるときに、親 DataGridViewの編集コントロールの一覧にコントロールを追加します。 また、セルのビジュアル プロパティの一部も初期化します。 たとえば、 InitializeEditingControl は、指定したセル スタイル パラメーターと一致するように編集領域の背景色を設定します。 InitializeEditingControlへの後続の呼び出しでは何も行われません。
派生クラスは、このメソッドを使用して、型に対応する Control クラスのインスタンスをホストします。 たとえば、1 つ以上のDataGridViewTextBoxCell オブジェクトを含むテーブルは、このメソッドを呼び出して、所有するDataGridViewに 1 つのTextBox編集コントロールを追加します。