Thursday, July 12, 2007

Showing/Hiding DataGridViewImageColumn Image

While developing an overview page that displays orders and their state, I needed to display a "Has been printed" Image after the order confirmation papers had been printed out. The intuitive thing to do for me was either to write a custom DataGridView cell type, or to use the DataGridViewImageColumn/Cell type. I was confident that the latter would lead me closer to my solution, although already suspecting that the Cell-Value would not be a boolean that indicates wether or not to show the image. So easily hiding the image wouldn't work.

So the only option i could come up with was to use (like in typical Web applications) a spacer image that simply consists of transparent pixel(s).

As the column was data-bound, I implemented a property that returned an image stored in my resources. It all might sound too simple and plain stupid, but there are quite a few people asking that question on the Net. So i post my property here, maybe it helps somebody:

public System.Drawing.Image WasPrintedImage
{
get
{
// Boolean property indicating the print-status
if (WasPrinted)
return ((System.Drawing.Image)(XY.Properties.Resources.PrintingDone));
else
return ((System.Drawing.Image)(XY.Properties.Resources.Spacer));
}
}