Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / February 2007

Tip: Looking for answers? Try searching our database.

Displaying a status image in a datagridview

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Simon Harvey - 10 Feb 2007 14:55 GMT
Hi all,

Can someone show me, or direct me to some code which deals with the
following (pretty standard) requirement:

I have a database column with an integer indicating status e.g 1 =
Available, 2 = Out of Stock and so forth.

In the datagrid, I would like to be able to display an appropriate
coloured icon according to the integer value.

The images are available in a resource file.

Can anyone suggest how I do this? Or at the very least tell me what
event I should use from the datagridview and whether I should be using a
databound column or not?

HUGE thanks to anyone who can advise

Kindest Regards

Simon
ClayB - 10 Feb 2007 15:34 GMT
Here is one way to do it. Just go ahead and have this column in your
DataGridView as a bound column. Then handle the CellPainting event and
draw the bitmap yourself instead of letting the grid to the default
drawing. Here is code that does this assuming the images have been
loaded into a ImageList.

//put some bitmaps into an ImageList
images = new ImageList();
images.Images.Add(SystemIcons.Error.ToBitmap());
images.Images.Add(SystemIcons.WinLogo.ToBitmap());
images.Images.Add(SystemIcons.Question.ToBitmap());

//this.images is an ImageList with your bitmaps
void dataGridView1_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{
   if (e.ColumnIndex == 1 && e.RowIndex > -1 && e.Value != null)
   {
       e.PaintBackground(e.ClipBounds, false);
       int index = (int) e.Value; // the 1, 2 or 3
       Point pt = e.CellBounds.Location;// where you want the bitmap
in the cell
       pt.X += 5;
       pt.Y += 1;
       this.images.Draw(e.Graphics, pt, index);
       e.Handled = true;
   }
}

================
Clay Burch
Syncfusion, Inc.
Bob Powell [MVP] - 11 Feb 2007 13:29 GMT
A DataGridView has an interesting feature. If you databind a column to a
byte array, it assumes that the byte array is an image from a database and
draws the image if it can.

Signature

Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

> Hi all,
>
[quoted text clipped - 18 lines]
>
> Simon
Simon Harvey - 11 Feb 2007 14:40 GMT
Thanks guys

Simon

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.