gpt4 book ai didi

c# - DataGridView 行在提交更改后仍然是脏的

转载 作者:太空狗 更新时间:2023-10-29 23:35:30 25 4
gpt4 key购买 nike

DataGridView.IsCurrentRowDirty遗迹 true在我提交对数据库的更改之后。我想将它设置为 false所以它不会触发 RowValidating当它失去焦点时。

我有一个 DataGridView绑定(bind)到 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.IsCurrentRowDirtytrue ?或者也许是一种防止 RowValidating 的方法避免不必要地验证所有单元格?

最佳答案

您是否尝试过在将数据保存到数据库后调用 DataGridView1.EndEdit()。

关于c# - DataGridView 行在提交更改后仍然是脏的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2043210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com