最近正在使用“DataGridView”對一個舊的Vs 2003開發的WINDOWS應用程序進行改造。
發現Vs 2003中的"DataGrid"中的一些事件已經在新的控件DataGridView中取消了,但是卻多了很多的“Cell”事件,真是被搞的頭大,主要是不了解各個事件的先后觸發順序。
所以寫了一個小程序,用來測試常用的Cell事件及順序。
第一種順序,即不進行Cell編輯的情況下:
CellEnter-發生於 DataGridView 單元格的焦點獲取的時候,或是單元格收到輸入焦點的時候。
CellLeave-發生於單元格失去輸入焦點時,而且現在是當前的單元格。
CellValidating-發生於單元格失去輸入焦點時,同時觸發數據驗證,進行數據驗證。
CellValidated –發生於單元格完成數據驗證之后。
各事件的觸發時間順序圖如下,由於CellEnter是第一個被觸發,后續事件,都是由人工去進行觸發的,所以時間間隔相對有點長。
第二種對單元格進行編輯之后的事件順序
CellEnter-發生於 DataGridView 單元格的焦點獲取的時候,或是單元格收到輸入焦點的時候。
CellBeginEdit –發生於選中的單元格進入編輯模式的時候。
CellLeave-發生於單元格失去輸入焦點時,而且現在是當前的單元格。
CellValidating-發生於單元格失去輸入焦點時,同時觸發數據驗證,進行數據驗證。
CellValueChanged-發生於單元格中的值發生變更時。
CellValidated -發生於單元格完成數據驗證之后。
CellEndEdit-發生於當前所選中的單元格退出編輯模式時。
各事件的觸發時間順序圖如下,由於CellEnter是第一個被觸發,后續事件,都是由人工去進行觸發的,所以時間間隔相對有點長。
測試代碼如下:
private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e) { txtCellLeave.Text = string.Format("Time={0};{1}事件,Row:col={2},{3}", DateTime.Now.ToString("HH:mm:ss.fff"), "CellLeave", e.RowIndex, e.ColumnIndex); System.Threading.Thread.Sleep(300); } private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e) { txtCellValidated.Text = string.Format("Time={0};{1}事件,Row:col={2},{3}", DateTime.Now.ToString("HH:mm:ss.fff"), "CellValidated", e.RowIndex, e.ColumnIndex); System.Threading.Thread.Sleep(300); } private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { txtCellEndEdit.Text = string.Format("Time={0};{1}事件,Row:col={2},{3}", DateTime.Now.ToString("HH:mm:ss.fff"), "CellEndEdit", e.RowIndex, e.ColumnIndex); System.Threading.Thread.Sleep(500); } private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) { txtCellBeginEdit.Text = string.Format("Time={0};{1}事件,Row:col={2},{3}", DateTime.Now.ToString("HH:mm:ss.fff"), "CellBeginEdit", e.RowIndex, e.ColumnIndex); System.Threading.Thread.Sleep(200); } private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { txtCellValidating.Text = string.Format("Time={0};{1}事件,Row:col={2},{3}", DateTime.Now.ToString("HH:mm:ss.fff"), "CellValidating", e.RowIndex, e.ColumnIndex); System.Threading.Thread.Sleep(200); } private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e) { txtCellEnter.Text = string.Format("Time={0};{1}事件,Row:col={2},{3}", DateTime.Now.ToString("HH:mm:ss.fff"), "CellEnter", e.RowIndex, e.ColumnIndex); System.Threading.Thread.Sleep(200); } private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) { txtCellValueChanged.Text = string.Format("Time={0};{1}事件,Row:col={2},{3}", DateTime.Now.ToString("HH:mm:ss.fff"), "CellValueChanged", e.RowIndex, e.ColumnIndex); System.Threading.Thread.Sleep(200); } WBK_COP_PDE pde = null; private WBK_COP_PDE ReadDataSource() { string path = string.Format("{0}\\{1}", Application.StartupPath, "WBK_COP_PDE_datasource.XML"); pde = XMLHelper.ParseXML<WBK_COP_PDE>(path, new WBK_COP_PDE()) as WBK_COP_PDE; return pde; } private void Form1_Load(object sender, EventArgs e) { WBK_COP_PDE pde = ReadDataSource(); BindData(); } private void BindData() { dataGridView1.DataSource = pde.WBK_PDE_LIST_ORG.WBK_PDE_ITEM_ORGS; }