?
Solved

C# DataGridViewImageColumn and ImageList

Posted on 2012-03-27
2
Medium Priority
?
2,466 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 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

762 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