Solved

C# DataGridViewImageColumn and ImageList

Posted on 2012-03-27
2
2,339 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable 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 viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

733 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