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

查看:4767
本文介绍了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:参数无效。在    System.Drawing.Image.FromStream(流流,布尔    useEmbeddedColorManagement,布尔validateImageData)

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

在我设置了我在Visual Studio设计的DataGridViewImageColumns,然后通过设置DataGridViewImageColumns的DataPropertyName属性相匹配类型的DataColumns绑定这些列DataColumns在一个DataTable:字节[]

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

这就是选择,我有去的,但现在我不是燮pressing异常的忠实粉丝,我看到的延迟在创造的DataGridView行因抛出+捕捉与处理程序的。

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(<一href="http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewimagecolumn.aspx">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;
            }
        }
    }

...没有工作。

...which didn't work.

另一种选择参与设置列CellTemplate于从DataGridViewImageColumn与空或一个的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?

推荐答案

最后,我所做的是我删除添加后右列(这将删除所有潜在的image列,你可能想自定义此,如果你的模式是不是动态的,你知道你要离开了什么):

Finally what I did is that I remove the column right after it is added (this 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 removing correction) of the columns. The exception didn't come that way.

此外,在你的 dataGridView1_RowsAdded 事件处理看出来,有不仅 e.RowIndex ,但 e.RowCount 过了,它可以是 e.RowCount&GT; 1 !所以一开始我尝试

Also, in your dataGridView1_RowsAdded event handler watch out that there's not only e.RowIndex but 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, and 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 rather removed the column!

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

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