c# - DataGridView - 单元格验证 - 防止 CurrentRow/Cell 更改

标签 c# winforms validation

我有 WinForms DataGridView,源设置为 SortableBindingList。在这种形式中,有 Comment 列,我需要防止用户插入一些字符,从而进行验证。

我想做的是,每当用户输入无效值时,系统都会通知他(OnNotification('您输入了错误的评论');)并强制他/她留在编辑模式。

到目前为止,我构建的解决方案是这样的:

void MyDataGridView_CellEndEdit( object sender, DataGridViewCellEventArgs e )
{
    if (e.ColumnIndex == ColumnComment.Index) {
        object data = Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
        if( (data != null) && (!CommentIsValid( data.ToString()))){
            CurrentCell = Rows[e.RowIndex].Cells[e.ColumnIndex];
            BeginEdit( true );

            // My notification method
            OnNotification( String.Format( "Comment `{0}` contains invalid characters) );
            return;
        }
    }
}

我有以下问题:

  • OnCellValidating 仅在整个表单关闭或当前行更改时触发,而不是在我完成单个单元格的编辑后触发,所以我检查了 CellEndEdit.
  • 当我使用 Enter/Esc 结束编辑时,它按预期工作。
  • 当我使用鼠标单击另一行时,单元格保持编辑模式,但另一行被选中。
  • 当我尝试使用 Enter(显示无效评论的通知)然后使用 Esc(取消编辑)时,它使用 pushed 的值Enter(因为编辑模式已经结束)。

所以我的问题是:

  • 如何在每次单元格编辑后触发 CellValidating,而不是在表单关闭时触发
  • 如何防止 CurrentRowCurrentCell 在鼠标单击后发生变化?
  • 如何强制单元格保持编辑模式?

最佳答案

When I use mouse and click to another row, cell stays in edit mode, but another row gets selected.

这里我会使用一个全局 bool 值,bool isInvalidState 和一个全局 DataGridViewCell = invalidCell 对象。在默认状态下,您可以设置 isInvalidState = falseinvalidCell = null。然后使用

private bool OnNotification(string cellValue)
{
    // Check for error.
    if (error)
        return false;
}

然后在上面的方法中

void MyDataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == ColumnComment.Index) {
        object data = Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
        if((data != null) && (!CommentIsValid(data.ToString()))){
            CurrentCell = Rows[e.RowIndex].Cells[e.ColumnIndex];
            BeginEdit(true);

            // My notification method
            isInvalidState = OnNotification(
                String.Format("Comment `{0}` contains invalid characters));
            if (isInvalidState)
                invalidCell = MyDataGridView[e.RowIndex, e.ColumnIndex];
            return;
        }
    }
}

现在,在您的 DataGridView 上连接一个事件 CellContentClick 并检查 isInvalidState == true

private void MyDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (isInvlaidState)
    {
        isInvalidState = false;
        MyDataGridView.CurrentCell = invalidCell;
        invalidCell = null;
        return;
    }
    // Do other stuff here.
}

When I try to use Enter (displays notification on invalid comment) and then Esc (to cancel edit) it uses value pushed by Enter (because edit mode has finished).

我不确定这个问题;您可能必须处理 KeyDown 事件并捕获转义键 - 以不同方式处理。

希望对您有所帮助。

关于c# - DataGridView - 单元格验证 - 防止 CurrentRow/Cell 更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15919504/

相关文章:

c# - 冰聊天应用

c# - DataGridView CellSelect 和 FullRowSelect

mysql - 日志轮换 MySQL Windows 批处理

c# - 从字典值中获取所有项目,这些值也是项目列表

c# - 将 iTextSharp 文档加载到 MemoryStream

c# - 密码登录异步 MVC C#

c# - 在 WebBrowser 中使用来自 CookieContainer 的 cookie

jquery - 当密码匹配时,我需要启用提交按钮。 (jQuery)

python - 在 Python 中验证名称的最佳方法

c# - 使用 this.validateChildren() 时如何针对所有无效表单控件设置 errorProvider?