【发布时间】:2010-01-11 16:36:22
【问题描述】:
DataGridView.IsCurrentRowDirty 在我提交对数据库的更改后仍然是true。我想将它设置为false,这样它就不会在失去焦点时触发RowValidating。
我有一个DataGridView 绑定到一个BindingList<T>。我处理CellEndEdit 事件并将更改保存到数据库。保存这些更改后,我希望将DataGridView.IsCurrentRowDirty 设置为true,因为该行中的所有单元格都是最新的;但是,它设置为false。
这会给我带来问题,因为当行确实失去焦点时,它会触发RowValidating,我会处理并验证其中的所有三个单元格。所以即使所有单元格都有效并且没有一个是脏的,它仍然会验证它们.太浪费了。
这是我所拥有的一个例子:
void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
// Ignore cell if it's not dirty
if (dataGridView.isCurrentCellDirty)
return;
// Validate current cell.
}
void dataGridView_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
{
// Ignore Row if it's not dirty
if (!dataGridView.IsCurrentRowDirty)
return;
// Validate all cells in the current row.
}
void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
// Validate all cells in the current row and return if any are invalid.
// If they are valid, save changes to the database
// This is when I would expect dataGridView.IsCurrentRowDirty to be false.
// When this row loses focus it will trigger RowValidating and validate all
// cells in this row, which we already did above.
}
我读过帖子说我可以调用表单的 Validate() 方法,但这会导致 RowValidating 触发,这是我试图避免的。
知道如何将DataGridView.IsCurrentRowDirty 设置为true 吗?或者也许是一种防止RowValidating 不必要地验证所有单元格的方法?
【问题讨论】:
标签: c# winforms validation datagridview