DataGridViewImageColumn triggers exception when assigning cell Value

211 Views Asked by At

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

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 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 color but triggers exception
    ...
}

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?

1

There are 1 best solutions below

0
AudioBubble On

Problem solved. See my final comment above.