【问题标题】:CancelEdit does not keep focus on edited cell in DataGridView c#CancelEdit 不专注于 DataGridView c# 中的已编辑单元格
【发布时间】:2013-12-29 04:51:22
【问题描述】:

当我在 DataGridView 的一个单元格中输入一些值并单击另一个单元格时,将执行 cellvalidating 事件处理程序代码。即使验证完成,我单击的单元格也会突出显示。我的要求是单元格应保持选中状态,并且在删除无效值后,光标应在单元格中闪烁以进行编辑。如果验证失败,我将使用以下代码:

DataGridView1.CancelEdit();

我已经尝试添加

DataGridView1.CurrentCell.Selected = true;
DataGridView1.BeginEdit(true);

【问题讨论】:

    标签: c# datagridview


    【解决方案1】:

    您需要使用e.Cancel = true,如下所示。

    private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {
        int i;
    
        if (!int.TryParse(e.FormattedValue.ToString(), out i))
        {
            e.Cancel = true;
            MessageBox.Show("Please input a integral number.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
    

    【讨论】:

      【解决方案2】:

      我用上面的解决方案稍微改变了我的代码,它起作用了:

      private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
      {
          DataGridView1.CancelEdit();
          e.Cancel = true;
          DataGridView.BeginEdit(true);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-25
        • 1970-01-01
        • 2015-08-03
        • 2015-01-11
        相关资源
        最近更新 更多