CancelEdit不保留对DataGridView中编辑的单元格c# [英] CancelEdit does not keep focus on edited cell in DataGridView c#

查看:148
本文介绍了CancelEdit不保留对DataGridView中编辑的单元格c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I enter some value in a cell in DataGridView and click on another cell the cellvalidating event handler code is executed. Even if validation fills, the cell on which I click gets highlighted. My requirement is that the cell should remain selected and cursor should blink in the cell for editing after removing the invalid value. Am using the below code if the validation fails:

DataGridView1.CancelEdit();

我已尝试添加

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


推荐答案

您需要使用 e

You need to use e.Cancel = true like below.

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);
    }
}

这篇关于CancelEdit不保留对DataGridView中编辑的单元格c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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