编辑datagridview时在哪里捕获未处理的异常 [英] Where to catch unhandled exception when editing datagridview

查看:128
本文介绍了编辑datagridview时在哪里捕获未处理的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据网格视图,该数据网格数据绑定到实体框架表:

I have a datagridview which is databound to an Entity Framework "table":

    public ObjectSet<TEntity> tableData { get; private set; }
    private BindingSource tableBinding;

    public AuxiliaryTableEditor(ObjectSet<TEntity> something)
    {
        InitializeComponent();

        this.tableData = something;
        this.tableBinding = new BindingSource();
        this.tableBinding.DataSource = tableData;

        this.auxiliaryTableEditorGridView.DataSource = tableBinding;
    }

这很好。

问题是这样的:如果用户开始编辑/添加一行,如果他们在单元格中进入编辑模式,则擦除内容,然后在其中进行制表或单击,然后<将抛出strong> unhandled 异常(因为大多数db列不允许为空)。这完全是正常且可以接受的,但我希望能够捕获并处理这些异常,并且我不知道在哪里/如何捕获它们

The problem is this: If a user starts editing / adding a row, if they enter into edit mode in a cell, erase the contents, then tab or click out of it, an unhandled exception will be thrown (as most of the db columns do not allow nulls). This is prefectly normal and acceptable, but I would like to be able to catch and handle these exceptions, and I don't know where / how to catch them.

我尝试在datagridview上使用几个不同的事件处理程序,例如。DataError 。RowValidating ,还有其他一些我现在不记得了……但是我似乎无法捕捉到讨厌的异常。

I have tried using several different event handlers on the datagridview, such as .DataError, .RowValidating, and a few others that I don't remember now... But I can't seem to catch the pesky exceptions.

任何建议都将不胜感激!

Any suggestions are greatly appreciated!

编辑:我应该补充一点,异常通常是这样的: ConstraintException未由用户代码处理。此属性无法设置为空值。

I should add that the exception is typically something like this: "ConstraintException was unhandled by user code. This property cannot be set to a null value".

推荐答案

尝试使用 CellEditEnding CurrentCellChanged 事件。

编辑:或者您可以

public int Number
{
  get { return _number; }
  set
  {  
      if (null == value)
      {
          // handle here
      }
  }
}

编辑:请参见提交空白字段时出现实体框架错误以获取更多信息。

See Entity Framework error when submitting empty fields for more info.

这篇关于编辑datagridview时在哪里捕获未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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