Solved

C# DataGridViewImageColumn and ImageList

Posted on 2012-03-27
2
2,243 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 500 total points
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now