Setting Image in DataGridView image column?

D

David Veeneman

I'm trying to set images in a bound DataGridView image column. My enum has
three possible values, and I have an image that I want to show for each
value:

EnumValue.OK --> GreenLight.gif
EnumValue.Caution --> YellowLight.gif
EnumValue.Violation --> RedLight.gif

I want to display the graphic, instead of the enum value, in each row of the
grid.

I tried to do this using the DataGridView CellFormatting event. I tried
taking the enum value from the cell Formatting event args (the e.Value
property), testing its value, then putting the appropriate image into the
e.Value property to display in the cell.

It's not working, because the DataGridView column is an image column and
won't take the enum value.

Here is what I'm trying to figure out:

(1) Am I going about this in the best way, or is there a different approach
that would work better? What would be involved in implementing it?

(2) If the CellFormatting event approach is viable, what do I need to do to
make it work?

Any and all suggestions appreciated. Thanks in advance.
 
D

David Veeneman

Here is the approach that I ultimately took. It's not as flexible as the
solution in Brian's book, but it has the virtue of simplicity, requiring
only a few lines of code to implement.

First, the column with the enum (the 'value column')must be included in the
grid, so that you can get the enum value for each row. So, set the Visible
property of that column to false, so it doesn't show.

Second, add an unbound image column to the grid, to act as the 'display
column'.

Third, create an event handler for the grid's DataBindingComplete event. We
could use the CellFormatting event, but that leads to continuous repainting
of the grid.

Fourth, add the following code to the DataBindingComplete event handler:

DataGridViewCell valueCell = null;
DataGridViewImageCell displayCell = null;
for (int row = 0; row < gridWidgets.Rows.Count; row++)
{
valueCell = gridWidgets[columnColor.Index, row];
displayCell = (DataGridViewImageCell)gridWidgets[columnGlyph.Index,
row];
int n = (int)valueCell.Value;
displayCell.Value = imageListWidgets.Images[n];
}

All the code does is read the value column and use it to set an image for
the display column.

Note that the code assumes that the form has an ImageList that has three
images, in the same order as the enum values. In real-world code, I wouldn't
bind the code that tightly to the image list.
 
D

David Veeneman

Oops--typo. In the last paragraph, I meant 'coupled' instead of 'bound'.
Production code would use a switch statement and would retrieve images from
the ImageList by key, rather than index. That way, the code doesn't depend
on the order of the images in the ImageList.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top