473,738 Members | 5,084 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Cauti on --YellowLight.gif
EnumValue.Viola tion --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 75050
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 DataBindingComp lete event. We
could use the CellFormatting event, but that leads to continuous repainting
of the grid.

Fourth, add the following code to the DataBindingComp lete event handler:

DataGridViewCel l valueCell = null;
DataGridViewIma geCell displayCell = null;
for (int row = 0; row < gridWidgets.Row s.Count; row++)
{
valueCell = gridWidgets[columnColor.Ind ex, row];
displayCell = (DataGridViewIm ageCell)gridWid gets[columnGlyph.Ind ex,
row];
int n = (int)valueCell. Value;
displayCell.Val ue = imageListWidget s.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
1611
by: jiatiejun | last post by:
How to bind PictureBox.Image to database image or bin column? thanks
0
1488
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. -- - Abhishek
4
2001
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
4740
by: DexHex | last post by:
Hi, I am stumpt. Is there anyway to set the background image on a webpart title?
3
94265
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 database. For example: table JobTypes 1 | Manager 2 | Controller 3 | Supervisor table Employee
0
3127
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 only cover the bottom 100px of the map (and the background to fill the rest of the space). The height of the main image will change, which is why I want to leave the empty space as a tiled background image. #my_map { display: block; height:...
0
1381
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 accessed from Webservice. The part of code to display the image in aspx page is added here. Bitmap bmp = new Bitmap(img, 100, 100); Graphics g = Graphics.FromImage(bmp); g.DrawImage(img, 100, 100);
0
2442
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) { column.ImageLayout = DataGridViewImageCellLayout.Stretch; column.Description = "Stretched"; } i get error on line 1 "DataGridViewImageColumn" Unable to cast object of type...
0
2853
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 details in data grid view.
0
8968
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9334
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9259
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6750
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4569
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.