DataGridView.BeginEdit(Boolean) 方法

定义

将当前的单元格置于编辑模式下。Puts the current cell in edit mode.

public:
 virtual bool BeginEdit(bool selectAll);
public virtual bool BeginEdit (bool selectAll);
abstract member BeginEdit : bool -> bool
override this.BeginEdit : bool -> bool
Public Overridable Function BeginEdit (selectAll As Boolean) As Boolean

参数

selectAll
Boolean

如果选择该单元格的所有内容,为 true;如果不选择任何内容,则为 falsetrue to select all the cell's contents; false to not select any contents.

返回

如果当前的单元格已经处于编辑模式或者成功进入编辑模式,则为 true;否则为 falsetrue if the current cell is already in edit mode or successfully enters edit mode; otherwise, false.

异常

CurrentCell 未设置为有效的单元格。CurrentCell is not set to a valid cell.

-或--or- 已在 CellBeginEdit 事件的处理程序中调用过此方法。This method was called in a handler for the CellBeginEdit event.

由单元格的 EditType 属性指示的类型并非派生自 Control 类型。The type indicated by the cell's EditType property does not derive from the Control type.

-或--or- 由单元格的 EditType 属性指示的类型并不实现 IDataGridViewEditingControl 接口。The type indicated by the cell's EditType property does not implement the IDataGridViewEditingControl interface.

编辑单元格值的初始化失败,DataError 事件没有处理程序,或者处理程序已将 ThrowException 属性设置为 trueInitialization of the editing cell value failed and either there is no handler for the DataError event or the handler has set the ThrowException property to true. 通常情况下,可将该异常对象强制转换为类型 FormatExceptionThe exception object can typically be cast to type FormatException.

示例

下面的代码示例演示如何使用此方法。The following code example demonstrates the use of this method.

// Override OnMouseClick in a class derived from DataGridViewCell to 
// enter edit mode when the user clicks the cell. 
protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
{
    if (base.DataGridView != null)
    {
        Point point1 = base.DataGridView.CurrentCellAddress;
        if (point1.X == e.ColumnIndex &&
            point1.Y == e.RowIndex &&
            e.Button == MouseButtons.Left &&
            base.DataGridView.EditMode !=
            DataGridViewEditMode.EditProgrammatically)
        {
            base.DataGridView.BeginEdit(true);
        }
    }
}
' Override OnMouseClick in a class derived from DataGridViewCell to 
' enter edit mode when the user clicks the cell. 
Protected Overrides Sub OnMouseClick( _
    ByVal e As DataGridViewCellMouseEventArgs)

    If MyBase.DataGridView IsNot Nothing Then

        Dim point1 As Point = MyBase.DataGridView.CurrentCellAddress
        If point1.X = e.ColumnIndex And _
            point1.Y = e.RowIndex And _
            e.Button = MouseButtons.Left And _
            Not MyBase.DataGridView.EditMode = _
            DataGridViewEditMode.EditProgrammatically Then

            MyBase.DataGridView.BeginEdit(True)

        End If
    End If
End Sub

注解

如果单元格未能进入编辑模式,此方法将返回 false,这可能是由于多种原因引起的。This method returns false if the cell fails to enter edit mode, which can happen for a number of reasons. 如果当前单元格为只读,则此方法返回 falseThis method returns false if the current cell is read-only. 如果单元 EditType 属性 null (表示单元格不能承载编辑控件)并且单元类型未实现 IDataGridViewEditingCell 接口,则它也会返回 falseIt also returns false if the cell EditType property is null (meaning the cell cannot host an editing control) and the cell type does not implement the IDataGridViewEditingCell interface.

如果单元支持编辑,此方法将引发 CellBeginEdit 事件,该事件可以取消,并在事件处理程序取消编辑时返回 falseIf the cell supports editing, this method raises the CellBeginEdit event, which can be canceled, and returns false if an event handler cancels the edit. 如果未取消编辑并且单元格可以承载编辑控件,则此方法将初始化控件并显示该控件。If the edit is not canceled and the cell can host an editing control, this method initializes the control and displays it. 如果初始化失败,则此方法返回 falseIf the initialization fails, this method returns false.

如果单元格成功进入编辑模式,则 IsCurrentCellInEditMode 属性将返回 trueIf the cell successfully enters edit mode, the IsCurrentCellInEditMode property returns true.

适用于

另请参阅