小编Kwe*_*ell的帖子

取消单元格验证并退出编辑模式

我的目标是在DataGridView上建立友好的验证流程。

当用户为某个单元格输入不正确的值时,我想要:

  • 退出编辑模式
  • 还原修改(即从单元格中恢复原始值)
  • 显示错误信息

我目前正在使用CellValidating事件来防止单元格更新其值,但是我无法退出编辑模式。然后该单元格正在等待正确的值,不会让用户简单地取消并还原其操作...

验证方法如下所示:

private void dataGridViewMsg_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
    [...] // Here, treatment determines is the new cell value isValid or not

    if (!isValid)
    {
        MessageBox.Show("The value entered is incorrect.", "Modification aborted");
        e.Cancel = true;
        dataGridViewMsg[e.ColumnIndex, e.RowIndex].IsInEditMode = false; // Someway, what I would like to do
        return;
    }

}
Run Code Online (Sandbox Code Playgroud)

如何在不要求我跟踪此值的情况下使单元恢复其原始值?

c# datagridview winforms

2
推荐指数
1
解决办法
6260
查看次数

标签 统计

c# ×1

datagridview ×1

winforms ×1