WinForm筆記1:TextBox編輯時和DataGridView 單元格編輯時 的事件及其順序


TextBox 編輯框

 

When you change the focus by using the mouse or by calling the Focus method, focus events occur in the following order:

Enter

GotFocus

LostFocus

Leave

Validating

Validated
---------------------

 

Cell單元格

第一種順序,即不進行Cell編輯的情況下:

CellEnter-發生於 DataGridView 單元格的焦點獲取的時候,或是單元格收到輸入焦點的時候。

CellLeave-發生於單元格失去輸入焦點時,而且現在是當前的單元格。

CellValidating-發生於單元格失去輸入焦點時,同時觸發數據驗證,進行數據驗證。

CellValidated –發生於單元格完成數據驗證之后。

 

第二種對單元格進行編輯之后的事件順序

CellEnter-發生於 DataGridView 單元格的焦點獲取的時候,或是單元格收到輸入焦點的時候。

CellBeginEdit –發生於選中的單元格進入編輯模式的時候。

CellLeave-發生於單元格失去輸入焦點時,而且現在是當前的單元格。

CellValidating-發生於單元格失去輸入焦點時,同時觸發數據驗證,進行數據驗證。

CellValueChanged-發生於單元格中的值發生變更時。

CellValidated -發生於單元格完成數據驗證之后。

CellEndEdit-發生於當前所選中的單元格退出編輯模式時。

應用

利用CellValidating事件 來驗證輸入數據,不合法就取消編輯。

 

private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
    //可編輯的列
    if (e.ColumnIndex != 2 && e.ColumnIndex != 3)
        return;
    double outDb = 0;
    if (double.TryParse(e.FormattedValue.ToString(), out outDb))
    {
        e.Cancel = false;
    }
    else
    {
        e.Cancel = true;//數據格式不正確則還原
        dataGridView1.CancelEdit();
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM