Solved

C# DataGridViewImageColumn and ImageList

Posted on 2012-03-27
2
2,321 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
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sort GridView by ID Descending 1 17
defining NULL or 0 10 44
c# - Best approach for objects in functions 3 24
need to convert function to c# 5 31
This article will show, step by step, how to integrate R code into a R Sweave document
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

831 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