提交更改后,DataGridView行仍然很脏 [英] DataGridView row is still dirty after committing changes

查看:351
本文介绍了提交更改后,DataGridView行仍然很脏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DataGridView.IsCurrentRowDirty 保留 true 我想将它设置为 false ,所以当它失去焦点时,它不会触发 RowValidating

DataGridView.IsCurrentRowDirty remains true after I commit changes to the database. I want to set it to false so it doesn't trigger RowValidating when it loses focus.

我有一个 DataGridView 绑定到一个 BindingList< T> 。我处理 CellEndEdit 事件并将更改保存到数据库。保存这些更改后,我希望 DataGridView.IsCurrentRowDirty 设置为 true ,因为该行中的所有单元格都已启动-至今;但是,它设置为 false

I have a DataGridView bound to a BindingList<T>. I handle the CellEndEdit event and save changes to the database. After saving those changes I would like DataGridView.IsCurrentRowDirty to be set to true, since all cells in the row are up-to-date; however, it's set to false.

这会导致我的问题,因为当行失去焦点时会触发 RowValidating ,我处理和验证所有三个细胞都在。所以即使所有的细胞都是有效的,没有一个是脏的,它仍然会验证它们。这是一个浪费。

This causes problems for me because when the row does lose focus it will trigger RowValidating, which I handle and validate all three cells in. So even though all the cells are valid and none are dirty it will still validate them all. That's a waste.

以下是我的一个例子:

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 触发,这是我想避免的。

I've read posts that said I could call the form's Validate() method, but that will cause RowValidating to fire, which is what I'm trying to avoid.

任何想法如何设置 DataGridView.IsCurrentRowDirty true ?或者可能是阻止 RowValidating 从不必要的验证所有单元格的方法?

Any idea how I can set DataGridView.IsCurrentRowDirty to true? Or maybe a way to prevent RowValidating from unnecessarily validating all the cells?

推荐答案

p>在将数据保存到数据库后尝试调用DataGridView1.EndEdit()。

Have you tried calling DataGridView1.EndEdit() after saving the data to the database.

这篇关于提交更改后,DataGridView行仍然很脏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆