【问题标题】:How to roll back changes in gridview in case of incorrect input在输入不正确的情况下如何回滚gridview中的更改
【发布时间】:2011-01-28 10:50:56
【问题描述】:

我有一个绑定到对象列表的 DataGridView。它有一些用户可以编辑的列。有某些输入是不允许用于整个行的。如果用户在某些单元格中输入无效输入,我该如何回滚。 我尝试使用 RowValidating 事件处理程序,但在更改单元格值后未调用它。即使我实现了 CellValueChanged,我仍然无法回滚更改。 ... 知道如何做到这一点

【问题讨论】:

    标签: c# winforms datagridview error-handling


    【解决方案1】:

    当数据绑定存在时,对我来说它适用于:

    myBindingSource.CancelEdit();
    myDataGridView.RefreshEdit();
    

    【讨论】:

    • datagridview.CancelEdit() 完美地为我工作!
    • 为我工作!没有 BindingSource,只是一个 DataGridView... dgvProductGroups.CancelEdit() dgvProductGroups.RefreshEdit()
    【解决方案2】:

    编辑完成并验证更改后,您可以执行以下操作:

    DataTable dt = this.dataGridView.DataSource as DataTable;
    dt.RejectChanges();
    

    来自MSDN

    当 DataTable.RejectChanges 方法被调用,任何行仍在 编辑模式取消他们的编辑。新行 被删除。修改和删除的行 回到原来的状态 (DataRowState.Unchanged)。

    【讨论】:

    • dt.RejectChanges() 给了我 NullReferenceExcpetion 尽管 DataSource 不为空
    • 表示无法将DataSource转化为DataTable。
    • 我刚刚做了myDataSet1.myDataTable.RejectChanges(); 我正在使用 BindingSource 作为我的 DGV 的数据源。非常感谢!它对我有用。
    【解决方案3】:

    您可以使用 CellValidating 事件在提交之前检查单元格的内容。如果你不喜欢它(不管你的验证规则是什么),你有几个选择。

    1) 您可以取消活动。用户在该行上看到一个错误图标,并且不能离开该单元格。他们被锁定在单元格编辑行为中,直到他们使用有效数据提交单元格(Enter、Tab)。

    2) 您可以将值回滚到另一个值(之前的值,一些默认值)。

    private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {
        DataGridView grid = sender as DataGridView;
        if (grid.Columns[e.ColumnIndex].HeaderText == "Text ID")
        {
            // Suppose you want to prevent them from leaving a cell when the text
            // in a specific column contains spaces:
    
            // value will hold the new data
            string value = (string)e.FormattedValue;
    
            if (value.Contains(" "))
            {
                grid.Rows[e.RowIndex].ErrorText = "String IDs cannot contain spaces.";
                // Setting e.Cancel will prevent them from leaving the cell.
                e.Cancel = true;
            }
        }
        else if (grid.Columns[e.ColumnIndex].HeaderText == "Platform")
        {
            // Or, suppose you have another column that can only contain certain values.
            // You could have used a ComboBoxColumn, but it didn't play with paste, or something.
            if (grid.EditingControl != null && (string)e.FormattedValue != "All")
            {
                // Going straight to the EditingControl will allow you to overwrite what
                // the user thought they were going to do.
    
                // Note: You don't want to e.Cancel here, because it will lock them
                // into the cell. This is just a brute force fix by you.
                string oldvalue = (string)grid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
                grid.EditingControl.Text = "All"; // or set it to the previous value, if you like.
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多