hello, i'm trying to check if a cell in datagridview is of type Image with the following code

if(cell.Value.GetType()==typeof(Image))
{
    ......
    ......
}

but it's always showing "Object reference not set to an instance of an object" error..could anytell me if i'm doing this right?? If not, how do we check if the cell contains objects of image type??

Recommended Answers

All 5 Replies

Did you set the column to be type of Image in the 1st place?

Seems like there is not a value in the cell, thus the object reference error. Debug and verify there is a value there.

Also Mitja's response is important as well.

Although this probably doesn't help your problem, there is a shorthand in c# for exactly what you are doing

if (cell.Value is Image)
{ ... }

Edit: I looked it up and your method actually runs faster. But if performance isn't what you're after then this is a handy shorthand :)

thank you guys..after all there was no value in the cell..i've been able to fix the problem now

you must specify the cell index, in which image is stored.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.