您的位置:首页 > 其它

DataGridView的Cell事件的先后触发顺序

2013-12-22 19:00 567 查看
最近正在使用“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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: