在可编辑单元格中写入值时,如何捕获DataGridView验证错误? [英] How to catch DataGridView validation Error when I write value in editable cells?

查看:117
本文介绍了在可编辑单元格中写入值时,如何捕获DataGridView验证错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到很多涉及数据绑定的解决方案,但是我没有数据源。在这种情况下,组合单元格仅适用于1行(其他行没有DataGridViewComboBoxCell)。

I've seen a lot of solutions for this where data binding is involved, but I don't have a data source. In this case the combo cell only applies to 1 row (other rows don't have the DataGridViewComboBoxCell).

我将DataGridViewComboCell设置如下:

I set a DataGridViewComboCell up like this:

DataGridViewComboBoxCell cell = new DataGridViewComboBoxCell();
cell.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
cell.Items.AddRange(items.ToArray());   // items is List<string>

然后我像下面这样动态地重新填充它:

And I dynamically re-populate it later like this:

_cell.Items.Clear();
_cell.Items.AddRange(this.Data.ResponseOptions.Select( d => d.Description).ToArray());   
//d.Description is of type string

但是随后我得到一个讨厌的对话框说:

But then I get this nasty dialog that says:


DataGridView中发生以下异常:
System.ArgumentException:DataGridViewComboBoxCell值无效。
要替换此默认对话框,请处理DataError事件。

The following exception occurred in the DataGridView: System.ArgumentException: DataGridViewComboBoxCell value is not valid. To replace this default dialog please handle the DataError event.

顺便说一句,说这不是有效。向MS发送一封电子邮件,说Windows窗体无效,这是否公平?

It doesn't help much, by the way, to say it is not "valid". Is it fair to send an email to MS saying Windows Forms is not valid?

我尝试抓取单元格的items属性,并使用foreach()与一个Add()调用。我仍然可以看到该对话框。

I've tried grabbing the cell's items property and adding the strings using a foreach() with an Add() call. I still get the dialog.

我还尝试每次想更新它时都删除整个单元格,并从头开始创建一个新的DataGridViewComboCell。我仍然可以看到对话框。

I've also tried blowing away the entire cell everytime I want to update it and recreating a new DataGridViewComboCell from scratch. I still get the dialog.

我也尝试过手动覆盖列的值(当我没有这个问题时会成功)。

I've also tried manually overwriting the columns value (succeeds when I don't have this problem). Didn't fix it, though.

我似乎只在尝试重新填充组合单元格中的项目时才看到此对话框。

I only seem to get this dialog when I'm trying to repopulate the items in the combo cell.

现在我只消灭了DataError方法。

For now I just wiped out the DataError method.

有任何建议吗?

推荐答案

这个问题有点老了,但我遇到了这个问题,事实证明ThunderGR绝对正确。当我让DataError打印报告时,我开始查看数据库中的值,并发现它们实际上在所有CAPS中。只需将我的字符串值更改为全部大写,就不再收到错误。

This question is a bit old but I ran into this and it turns out that ThunderGR absolutely had it right. When I had DataError print a report I began to look at the values in the database and noticed that they actually were in all CAPS. Simply changed my string values to all caps and no longer received the error.

这是一种可能处理DataError的方法,尽管我宁愿不要将其触发

Here is a way of possibly handling the DataError although I'd rather not have it fired in the first place.

    private void dataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
    {
        try
        {
            if (e.Exception.Message == "DataGridViewComboBoxCell value is not valid.")
            {
                object value = dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
                if (!((DataGridViewComboBoxColumn)dataGridView.Columns[e.ColumnIndex]).Items.Contains(value))
                {
                    ((DataGridViewComboBoxColumn)dataGridView.Columns[e.ColumnIndex]).Items.Add(value);
                }
            }

            throw e.Exception;
        }
        catch (Exception ex)
        {
            bool rethrow = ExceptionPolicy.HandleException(ex, "UI Policy");
            if (rethrow)
            {
                MessageBox.Show(string.Format(@"Failed to bind ComboBox. "
                + "Please contact support with this message:"
                + "\n\n" + ex.Message));
            }
        }
    }

这样,值将得到已添加到项目集合中,但仍抛出可记录日志的异常。我使用了消息框,因为我仍然认为用户遇到错误消息并与支持人员联系以对其进行修复很重要。希望这对某人有帮助!

This way the value will get added to the items collection but still throw the exception where it can be logged. I used a messagebox because I still think it is important that the user experience the error message and contact support to have it fixed. Hope this helps someone!

这篇关于在可编辑单元格中写入值时,如何捕获DataGridView验证错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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