本文介绍了DataGridViewImageColumn在分配单元格值时触发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


使用DataGridView的C#表单,第一列是DataGridViewImageColumn。在表单构造函数中,向DataGridView添加行,第一个参数是从文件读取的Bitmap对象。 DataGridView正确显示,没有问题。

Have C# form with DataGridView with first column being DataGridViewImageColumn. In form constructor, Add rows to DataGridView, with first argument being a Bitmap object read from a file. DataGridView displays correctly, no problems.

Bitmap blackCamera = null;
Bitmap grayCamera = null;
public FormConstructor()
{
    ...
    blackCamera = new Bitmap(@"BlackCamera.bmp");
    grayCamera = new Bitmap(@"GrayCamera.bmp");
    ...
    int row = dataGridView_xxx.Rows.Add(blackCamera); // works fine
    ...
    dataGridView_xxx.Rows[n].Cells[0].Value = grayCamera; // works fine
    ...
}

稍后,想要更改某些行的第一个列单元格的图像。在鼠标函数的回调中,如果尝试分配DataGridViewImageColumn单元格值,我得到ThreadExceptionEvent异常"无法将类型为System.Drawing.Bitmap
的对象强制转换为类型System.String"但图像确实会更改为所需的新位图。例如:

Later, want to change image for some row's first column cell. Inside a callback for a mouse function, if attempt to assign the DataGridViewImageColumn cell Value, I get ThreadExceptionEvent exception "unable to cast object of type System.Drawing.Bitmap to type System.String" but the image does change to the desired new bitmap. For example:

private void MouseCallback(Object sender, EventArgs e)
{
    ...
    dataGridView_xxx.Rows[nnn].Cells[0].Value = blackCamera; // changes image but triggers exception
    ...
}

Microsoft文档似乎支持如上所述分配DataGridViewImageColumn单元格值。奇怪的是,它在构造函数中毫无例外地工作,但在回调中处理异常。如何消除异常?

Microsoft documentation appears to support assigning DataGridViewImageColumn cell value as above. Curiously, it works without exception in the constructor, but works with exception in the callback. How to eliminate the exception?

此外,在xxx.Value的每次赋值之前和之后放置xxx.Value.GetType()一致地显示System.Drawing.Bitmap而不是System.String。那么为什么框架将System.Drawing.Bitmap结束为System.String强制转换是必要的呢?

Also, placing xxx.Value.GetType() before and after each assignment of xxx.Value consistently shows System.Drawing.Bitmap not System.String. So why is framework concluding a System.Drawing.Bitmap to System.String cast is necessary?

推荐答案

>>无法
将System.Drawing.Bitmap类型的对象强制转换为System.String

>>unable to cast object of type System.Drawing.Bitmap to type System.String

你在哪里调用代码中的 MouseCallback 事件?

Where did you call the MouseCallback event in your code?

我测试了代码,无法像你一样重现问题:

I tested the code and could not reproduce the issue as you:

        public Form1()
        {
            InitializeComponent();
            blackCamera = new Bitmap(@"D:\MyPicture\77.jpg");
            grayCamera = new Bitmap(@"D:\MyPicture\66.jpg");
            int row = dataGridView1.Rows.Add(blackCamera); // works fine
            dataGridView1.Rows[0].Cells[0].Value = grayCamera; // works fine
        }
        Bitmap blackCamera = null;
        Bitmap grayCamera = null;

        private void button1_Click(object sender, EventArgs e)
        {
            MouseCallback(sender, e);
        }

        private void MouseCallback(Object sender, EventArgs e)
        {
            dataGridView1.Rows[0].Cells[0].Value = blackCamera;
        }

问候,

Stanly


这篇关于DataGridViewImageColumn在分配单元格值时触发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 23:10