C# DataGridViewImageColumn and ImageList
Posted on 2012-03-27
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 is my ItemID
-Cell 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.Value != null)
row.Cells.Value = imageList1.Images[int.Parse(row.Cells.Value.ToString())];
Thanks in advance!