Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

C# DataGridViewImageColumn and ImageList

Posted on 2012-03-27
2
Medium Priority
?
2,604 Views
Last Modified: 2012-08-13
Hey,

I'm a beginner C# developer. I've been trying to figure out the most efficient way of adding about 300 small image like icons to a DataGridView.

I have a SQL Database with a Primary Key of ItemID. And what I would like to do is match the ItemIDs (1,2,3,4,5,6,etc) with an ImageList (0,1,2,3,4,56,etc)

Here is the meat and potatoes of what I made so far. Just a button for debugging purposes which will load the images from the Imagelist into the DataGridView. The only problem I have so far is that my imagelist starts at index 0 and my DataGridView at 1 so the 0 is wasted in the imagelist. Basically I am matching up the appropriate image with the appropriate ItemID.But i'm guessing there is probably better ways of doing this? I am trying to avoid code such as "if item name = car" then load car.jpg" and have 300 if/select case statements.

-Cell[0] is my ItemID
-Cell[7] is the column I update with the appropriate image.

private void button1_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in itemsDataGridView.Rows)
            {
                if (row.Cells[0].Value != null)
                {
                row.Cells[7].Value = imageList1.Images[int.Parse(row.Cells[0].Value.ToString())];
                }
            }

Thanks in advance!
0
Comment
Question by:soulhero
2 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 1000 total points
ID: 37775129
Just subtract one of the itemID to get the right image

row.Cells[7].Value = imageList1.Images[int.Parse(row.Cells[0].Value.ToString())-1];
0
 
LVL 1

Author Comment

by:soulhero
ID: 37780189
Thanks. Got one small issue. The first image shows up but if I click on it or off it. The image will turn into a white box with a red x.

So after playing around with it a bit. I noticed that by default row 1 is selected. (Blue Highlighted) on form load. But If I click off that row then run the button it works fine. I tried taking focus off the DataGrid and adding  itemsDataGridView.ClearSelection(); after the form loaded but this did not work. I can now replicate the issue with any selected cell then clicking the button... that cell looses the image after focus is taken off..

I finally figured out my issue. I was filling my DataGridView with a TableAdapter and also Data Binding the cells to several text boxes. I just filled the DataGridView, ran the loop to add the imagelist to the cells, then did the databinding after instead of before and now its working fine.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses
Course of the Month21 days, 5 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question