How to add images to DataGridView cell using C# and Windows Forms
There are many ways to skin this old cat. When choosing the way, you must first decide when should the adding of images take place. I chose to add images during sorting.
Let us assume you already added some data to gridview via datatable using ;
dataGridView1.DataSource = siparis_tablo_guncel;
So now you know we have a filled up dataGridView.
Now let us assume in the dataGridView1 we want to add images according to the data within the 6th column. So what we first need to do is to create an image column by writing;
DataGridViewImageColumn img = new DataGridViewImageColumn();
img.Name = "img";
img.HeaderText = "Image Column";
img.ValuesAreIcons = true;
dataGridView1.Columns.Add(img);
Now we have an image column. With the name img
with the header “Image Column” As you can see on the 5th line I have stated that my images are actually icons. The reason I did this was that I found out that icons take up less space and flicker less. You can if you want to change this option as you see fit.
Now let us get cracking and add those images according to the columns;
int number_of_rows = dataGridView1.RowCount;
for (int i = 0; i < number_of_rows; i++)
{
if (dataGridView1.Rows[i].Cells[6].Value.ToString() == "true") {
Icon image = SUT.Properties.Resources.succcess_icon;
dataGridView1.Rows[i].Cells["img"].Value = image;
} else {
Icon image = SUT.Properties.Resources.cancel_icon;
dataGridView1.Rows[i].Cells["img"].Value = image;
}
}
I hope this has been informative. Please do tell if you see any mistakes and wish to make suggestions.
Have fun!