DataGridView.BeginEdit(Boolean) 方法

定義

將目前儲存格置於編輯模式下。

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 表示選取所有儲存格的內容,false 則表示不選取任何內容。

傳回

如果目前儲存格已經處於編輯模式下,或成功進入編輯模式,則為 true;否則為 false

例外狀況

CurrentCell 未設定為有效的儲存格。

-或-

這個方法是在 CellBeginEdit 事件的處理常式中所呼叫。

儲存格的 EditType 屬性所表示的類型不是衍生自 Control 類型。

-或-

儲存格的 EditType 屬性所表示的類型未實作 IDataGridViewEditingControl 介面。

編輯儲存格值的初始設定失敗,而且 DataError 事件沒有處理常式或處理常式已將 ThrowException 屬性設定為 true。 例外狀況物件通常可轉換成 FormatException 類型。

範例

下列程式碼範例示範如何使用這個方法。

// 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 ,原因可能有很多。 如果目前儲存格是唯讀的,這個方法會傳 false 回 。 如果儲存格 EditType 屬性 null (,則也會傳回 false ,這表示儲存格無法裝載編輯控制項) ,而且儲存格類型不會實作 IDataGridViewEditingCell 介面。

如果儲存格支援編輯,這個方法會 CellBeginEdit 引發事件,可以取消此事件,如果事件處理常式取消編輯,則會傳回 false 。 如果未取消編輯,而且儲存格可以裝載編輯控制項,這個方法會初始化控制項並顯示它。 如果初始化失敗,這個方法會傳 false 回 。

如果儲存格成功進入編輯模式,屬性會 IsCurrentCellInEditModetrue 回 。

適用於

另請參閱