DataGridView由于DataGridViewImageColumn而导致默认错误对话框中的异常 [英] DataGridView throwing Exception in Default Error Dialog due to DataGridViewImageColumn

查看:557
本文介绍了DataGridView由于DataGridViewImageColumn而导致默认错误对话框中的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把它放在一起,因为在网络上找到答案花了太长时间,这可能是一个常见的问题 - 这是我第二次在我的应用程序中体验到它。

I am putting this up because it took far too long to find the answer on the web and this is probably a common problem - it is the second time i have experienced it on my app.

当具有DataGridViewImageCell的新行变为可见,并且没有设置默认值时,DataGridView将抛出以下异常:

When a new row with a DataGridViewImageCell becomes visible, and it has no Default value set, my DataGridView throws the following Exception:


以下异常发生在DataGridView中:

The Following Exception occurred in the DataGridView:

System.ArgumentException:参数无效。 at
System.Drawing.Image.FromStream(Stream stream,Boolean
useEmbeddedColorManagement,Boolean validateImageData)

System.ArgumentException: Parameter is not valid. at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)"

在我的设置中,我在Visual Studio Designer中创建DataGridViewImageColumns,然后通过将DataGridViewImageColumns的DataPropertyName属性设置为匹配DataColumns类型:byte []来将这些列绑定到DataTable中的DataColumns。

In my set up I create the DataGridViewImageColumns in Visual Studio Designer and then bind these columns to DataColumns in a DataTable by setting the DataPropertyName Properties of the DataGridViewImageColumns to match DataColumns of Type: byte[].

但是,当新行中的DataGridViewImageColumn变为可见时,它仍会抛出此异常。

However, it still throws this Exception when the DataGridViewImageColumn in the new Row becomes visible.

有两种解决方法适用于我:

There are two workarounds that worked for me:


  1. 在设计器中取消选中启用添加选项,然后以编程方式添加行 - 使用按钮等 - 我认为这是我第一次做的。

  2. 处理DataGridView的DataError事件,如下所示:

  1. Uncheck the "Enable Adding" option in the Designer - then add rows programmatically - using buttons etc. - I think this is what I did first time round.
  2. Handle the DataError Event of the DataGridView like this:

    private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
    {
        if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == DBNull.Value)
        {
            e.Cancel = true;
        }
    }




< >这是我现在所选择的选项,但我不是抑制异常的粉丝,我可以看到DataGridView行的创建延迟,因为处理程序抛出+ Catch。

That's the option I am going with for now but I'm not a fan of suppressing Exceptions and I can see the delay in the creation of the DataGridView row due to throw + Catch by the Handler.

MSDN( http:// msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewimagecolumn.aspx )说您可以处理RowsAdded事件并强制空值。我试过这样:

MSDN (http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewimagecolumn.aspx) says that you can handle the RowsAdded event and force a null value. I tried this:

    private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
    {
        foreach (DataGridViewCell cell in dataGridView1.Rows[e.RowIndex].Cells)
        {
            if (cell.GetType() == typeof(DataGridViewImageCell))
            {
                cell.Value = DBNull.Value;
            }
        }
    }

另一个选项涉及将Column CellTemplate设置为从DataGridViewImageColumn派生的类型,默认值为null或DBNull.Value。

The other option involved setting the Column CellTemplate to a Type derived from DataGridViewImageColumn with a default value of null or DBNull.Value.

现在有点迟了 - 我一整天都在这里。

It's bit late for that now - I've been at this all day.

我可能要去找我的选项2 ,但任何人都可以告诉我如何使选项3/4工作?有一个最好的方法吗?

I'm probably going to go for my option 2, but can anyone tell me how to get option 3/4 to work? Is there a best approach for this?

推荐答案

我的解决方案删除列后添加(详细原因在结束)。以下代码将删除所有可能的图像列,如果您的模式不是动态的,您可能需要自定义这些列,并且您知道您想要删除的内容:

My solution to remove the column right after it is added (detailed reason at the end). The following code removes all potential image columns, you may want to customize this if your schema is not dynamic and you know what you want to leave out:

public Form1()
{
    InitializeComponent();
    dataGridView1.ColumnAdded += dataGrid_ColumnAdded;
}

void dataGrid_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
{
    if (e.Column.CellType == typeof(DataGridViewImageCell))
        dataGridView1.Columns.Remove(e.Column);
}

所以当涉及到实际的绑定

so when it comes to the actual binding

DataTable table = dataTableCombo.SelectedItem as DataTable;
dataGridView1.DataSource = table;

填充单元格将在添加(和删除更正)列之后发生。而且这个例外并不是这样的。

populating the cells will happen after adding (and the removing correction) of the columns. And the exception doesn't happen this way.

另外,在你的 dataGridView1_RowsAdded 事件处理程序中注意:没有只有 e.RowIndex ,但也有 e.RowCount ,它可以是 e.RowCount > 1 !所以首先我尝试:

Also, in your dataGridView1_RowsAdded event handler watch out: there's not only e.RowIndex but there's e.RowCount too, and it can be e.RowCount > 1! So first I tried:

void dataGrid_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
    for (int i = e.RowIndex; i < e.RowIndex + e.RowCount; i++)
    {
        foreach (DataGridViewCell cell in dataGridView1.Rows[i].Cells)
        {
            if (cell.GetType() == typeof(DataGridViewImageCell))
            {
                cell.Value = DBNull.Value;
            }
        }
    }
}

我还有一些例外。另外,如果你的绑定是双向的,注意因为 cell.Value = DBNull.Value; 导致业务对象的变化!这就是我建议只是删除列的原因。

but I still got some exceptions. Plus if your binding is two-way, watch out because cell.Value = DBNull.Value; causes a change in the business object! That's the reason I advise to just rather remove the column.

这篇关于DataGridView由于DataGridViewImageColumn而导致默认错误对话框中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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