DataGridViewCell.InitializeEditingControl(Int32, Object, DataGridViewCellStyle) 方法

定义

初始化用于编辑单元格的控件。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

单元格所在位置的从零开始的行索引。The zero-based row index of the cell's location.

initialFormattedValue
Object

Object,它表示在开始编辑时单元格显示的值。An Object that represents the value displayed by the cell when editing is started.

dataGridViewCellStyle
DataGridViewCellStyle

表示单元格样式的 DataGridViewCellStyleA 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.

示例

下面的代码示例演示如何重写从 DataGridViewTextBoxCell 类派生的简单类中的 InitializeEditingControl 方法。The following code example demonstrates how to override the InitializeEditingControl method in a simple class that derives from the DataGridViewTextBoxCell class. 此示例摘自 如何:中提供的一个更大的代码示例Windows 窗体 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 对象的表将调用此方法来向所属 DataGridView添加单个 TextBox 编辑控件。For example, a table that contains one or more DataGridViewTextBoxCell objects calls this method to add a single TextBox editing control to the owning DataGridView.

适用于

另请参阅