【问题标题】:DataGridView row is still dirty after committing changes提交更改后 DataGridView 行仍然脏
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

    • 是的,那没用。当行失去焦点时,RowValidating 仍然会触发。工具提示说 EndEdit() 适用于当前单元格。如果有类似的东西,比如 EndRowEdit,我可能会很幸运。不过感谢您的建议!
    • 如果有人像我一样遇到过这种情况。我通过调用datagrid.currentrow.datagrid.endedit() 然后datagrid.endedit 和最后bindingSource.endedit() 解决了这个问题
    【解决方案2】:

    Validating 触发两次时我遇到了同样的问题。在提交编辑之前一次(如预期的那样),但随后又是我认为DataGridView 焦点发生变化。

    没有时间调查。快速修复是 this.ActiveControl = null; 我不确定这是否有任何意想不到的后果,但它通过以编程方式使控件失焦来修复验证问题。

    private void cntrl_MethodParameters_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {
        //Init
        var dgv = (DataGridView)sender;
        int row = e.RowIndex;
        int col = e.ColumnIndex;
    
        //Validate Edit
        if ((row >= 0) && (col == cntrl_MethodParameters.Columns.IndexOf(cntrl_MethodParameters.Columns[MethodBuilderView.m_paramValueCol])))
        {
            string xPropertyName = (string)cntrl_MethodParameters[MethodBuilderView.m_paramNameCol, row].EditedFormattedValue;
            string xPropertyValue = (string)cntrl_MethodParameters[MethodBuilderView.m_paramValueCol, row].EditedFormattedValue;
            bool Validated = FactoryProperties.Items[xPropertyName].SetState(xPropertyValue);
    
            //Cancel Invalid Input
            if (!Validated)
            {
                dgv.CancelEdit();
            }
        }
        this.ActiveControl = null;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-19
      • 1970-01-01
      • 2012-08-25
      • 1970-01-01
      • 2011-04-10
      • 1970-01-01
      • 2018-11-02
      • 1970-01-01
      相关资源
      最近更新 更多