1,105,290 Community Members

typeof(Image)

Member Avatar
cool_zephyr
Junior Poster
179 posts since Apr 2009
Reputation Points: 7 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 2 [?]
 
0
 

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??

Member Avatar
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 557 [?]
Q&As Helped to Solve: 489 [?]
Skill Endorsements: 21 [?]
 
0
 

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

Member Avatar
jbrock31
Junior Poster in Training
79 posts since Oct 2008
Reputation Points: 11 [?]
Q&As Helped to Solve: 17 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
skatamatic
Posting Shark
980 posts since Nov 2007
Reputation Points: 371 [?]
Q&As Helped to Solve: 133 [?]
Skill Endorsements: 5 [?]
 
0
 

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 :)

Member Avatar
cool_zephyr
Junior Poster
179 posts since Apr 2009
Reputation Points: 7 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 2 [?]
 
0
 

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

Question Answered as of 2 Years Ago by Mitja Bonca, skatamatic and jbrock31
Member Avatar
vimit
Newbie Poster
15 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

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

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: