Share via


Add Images to DatagridView Cell

Question

Thursday, December 25, 2014 11:23 AM

After i bind DatagridView to datasource i add three columns to gridview and the added syntax is :

            DataGridViewImageColumn imgEnroll = new DataGridViewImageColumn();
            imgEnroll.Name = "imgEnroll";
            imgEnroll.HeaderText = "ENROLL";
            imgEnroll.ValuesAreIcons = true;
            imgEnroll.Width = 50;
            dataGridView1.Columns.Add(imgEnroll);

            DataGridViewImageColumn img = new DataGridViewImageColumn();
            img.Name = "img";
            img.HeaderText = "EDIT";
            img.ValuesAreIcons = true;
            img.Width = 50;
            dataGridView1.Columns.Add(img);

            DataGridViewImageColumn img1 = new DataGridViewImageColumn();
            img1.Name = "img1";
            img1.HeaderText = "DELETE";
            img1.ValuesAreIcons = true;
            img1.Width = 50;
            dataGridView1.Columns.Add(img1);

            int number_of_rows = dataGridView1.RowCount;
            for (int i = 0; i < number_of_rows; i++)
            {
                Image imageEnroll = global::iBrain.Properties.Resources.add_button;
                DataGridViewImageCell enrollIcon = new DataGridViewImageCell();
                enrollIcon.Value = imageEnroll;
                dataGridView1.Rows[i].Cells["imgEnroll"].Value = enrollIcon;

                Icon image = iBrain.Properties.Resources.edit_icon1;
                dataGridView1.Rows[i].Cells["img"].Value = image;

                Icon image1 = iBrain.Properties.Resources.delete_icon2;
                dataGridView1.Rows[i].Cells["img1"].Value = image1;
            }

Everything going correct but image is not displaying . Can anybody help me to shutout this issue ?

All replies (2)

Friday, December 26, 2014 7:26 AM âś…Answered

Hi BiswoThapa,

After some research, i got your main issue is first we often add Image to C# DataGridView use

DataGridViewImageColumn like the following sample code. You should be adding Image objects, such as a Bitmap made from that "start.bmp", not a DataGridViewImageCell.So don't use DataGridViewImageCell in your for loop. It will throw exception.

 DataGridViewImageColumn enrollIcon = new DataGridViewImageColumn();
                enrollIcon.Width = 100;
                  
                Image imageEnroll = DataGridComboBoxDemo.Properties.Resources.add_button;

                enrollIcon.Image = imageEnroll;
               // dataGridView1.Rows[i].Cells["imgEnroll"].Value = imageEnroll;
                dataGridView1.Columns.Add(enrollIcon);

Second DataGridViewImageColumn.Image Property is gets or sets the image displayed in the cells of this column when the cell's Value property is not set and the cell's ValueIsIcon property is set to false.

So in DataGridView we must add at least one row. The image will not display if there is no rows.  After modify your code. Here is the working samle.

 private void button4_Click(object sender, EventArgs e)
        {
            dataGridView1.ColumnCount = 3;
            dataGridView1.Columns[0].Name = "Product ID";
            dataGridView1.Columns[1].Name = "Product Name";
            dataGridView1.Columns[2].Name = "Product Price";

            string[] row = new string[] { "1", "Product 1", "1000" };
            dataGridView1.Rows.Add(row);


            DataGridViewImageColumn img = new DataGridViewImageColumn();
            img.Name = "img";
            img.HeaderText = "EDIT";
            img.ValuesAreIcons = true;
            img.Width = 50;
            dataGridView1.Columns.Add(img);

        
            int number_of_rows = dataGridView1.RowCount;
            for (int i = 0; i < number_of_rows; i++)
            {
                DataGridViewImageColumn enrollIcon = new DataGridViewImageColumn();
                enrollIcon.Width = 100;
                  
                Image imageEnroll = DataGridComboBoxDemo.Properties.Resources.add_button;

                enrollIcon.Image = imageEnroll;
    
                dataGridView1.Columns.Add(enrollIcon);
  

               Icon image = DataGridComboBoxDemo.Properties.Resources.edit_icon1;
                dataGridView1.Rows[i].Cells["img"].Value = image;
            }
        }

Have a nice day!

Kristin

We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.


Sunday, December 28, 2014 6:55 AM

Frankly you your UI is image intensive then don't go for Winform but WPF.

Check this https://chanmingman.wordpress.com/2011/06/21/bind-image-from-ms-sql-server-2008-binary-field-to-wpf-listbox/

chanmm 

chanmm