473,387 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Setting Image in DataGridView image column?

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.

--
David Veeneman
Foresight Systems
Jul 24 '06 #1
3 74994
I found an answer in Brian Noyes' book, Data Binding with Windows Forms 2.0:
Programming Smart Client Data Applications with .NET. Chapter 6, "Presenting
Data with the DataGridView Control in .NET 2.0", describes how to create a
custom column type to display enumerated graphics. The chapter can be found
online at
http://www.awprofessional.com/articl...seqNum=14&rl=1.

--
David Veeneman
Foresight Systems
Jul 24 '06 #2
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.

--
David Veeneman
Foresight Systems
Jul 24 '06 #3
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.

--
David Veeneman
Foresight Systems
Jul 24 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: jiatiejun | last post by:
How to bind PictureBox.Image to database image or bin column? thanks
0
by: Abhishek Bagga | last post by:
I want to insert Image in the column headers of the Listview control I have been trying this but was unable to achieve it. Is it possible? if yes kindly guide me how to achieve this. -- -...
4
by: Sparky Arbuckle | last post by:
Does anyone know of a way to add an image into a DataGrid's Template Column? Any suggestions are greatly appreciated.
3
by: DexHex | last post by:
Hi, I am stumpt. Is there anyway to set the background image on a webpart title?
3
by: sklett | last post by:
I'm changing from a DataGrid to a DataGridView and have run across a problem. The items that are bound to the DataGrid have an int Property that represents a primary key of a lookup table in my...
0
by: aparth | last post by:
I've been trying and failing to create an image map that contains a tiled background image underneath the main image. My image map is 600px in height. I want the main image (img_bottom.jpg) to...
0
by: manjitsarma | last post by:
I need to display an image in 'Image Control' of aspx page.Now the image is displayed in the aspx page itself.But I need to display it in 'Image Control'.This is a map application and the image is...
0
by: dotnetnovice | last post by:
Hi i am using this code to dispaly images from database to datagridview, All code lines works well except these foreach (DataGridViewImageColumn column in dataGridView1.Columns) { ...
0
by: visweswaran2830 | last post by:
Hi, I am having datagridview in that I have datagridview combobox column and noraml textfield. I want to know that how can I load selected value in that combox box when I am loading all entered...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.