C# DataGridView - ImageColumn,ComboBoxColumn,CheckBoxColumn

June 27, 2017 Oclemy C# DataGridView, C# ComboBox, C# CheckBox 1 minute, 27 seconds

In this example we use DataGridView with an ImageColumn,CheckBoxColumn as well as ComboBoxColumn.Of course also textboxcolumn. This is what we do in short :

  • The ImageColumn in our datagridview helps us render images in our datagridview. We load the images from our local filesystem.
  • The ComboBox column displays data in dropdown manner.We render it in our DataGridview.
  • The CheckBoxColumn renders boolean values in our datagridview.

Here's the code :  

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DGView_Images_Combo_CheckBox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            fillData();
        }

        private void fillData()
        {
            //CONSTRUCT TEXT COLUMNS
            dataGridView1.ColumnCount = 1;
            dataGridView1.Columns[0].Name = "Name";

            //CONSTRUCT CHECKBOX COLUMN
            DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
            chk.HeaderText = "Over 25 ??";
            chk.Name = "Age";
            dataGridView1.Columns.Add(chk);

            //CONSTRUCT IMAGE COLUMN
            DataGridViewImageColumn imgCol = new DataGridViewImageColumn();
            imgCol.HeaderText = "Photo";
            imgCol.Name = "Photo";
            dataGridView1.Columns.Add(imgCol);

                      //ADD DATA
            //FIRST ROW
            //LOAD IMAGE
            Image img = Image.FromFile(@"C:/Imgs/vanpersie.jpg");
            Object[] row = new Object[] { "Van Perise", true, img };
            dataGridView1.Rows.Add(row);

            //2nd ROW
            img = Image.FromFile(@"C:/Imgs/herera.jpg");
            row = new Object[] { "Ander Herera", false, img };
            dataGridView1.Rows.Add(row);

            //3rd ROW
            img = Image.FromFile(@"C:/Imgs/hazard.jpg");
            row = new Object[] { "Eden Hazard", false, img };
            dataGridView1.Rows.Add(row);

            //4th ROW
            img = Image.FromFile(@"C:/Imgs/ramsey.jpg");
            row = new Object[] { "Aaron Ramsey", true, img };
            dataGridView1.Rows.Add(row);

            //5th ROW
            img = Image.FromFile(@"C:/Imgs/oscar.jpg");
            row = new Object[] { "Oscar", false, img };
            dataGridView1.Rows.Add(row);

            //CONSTRUCT COMBOBOX COLUMN
            DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
            combo.HeaderText = "Position";
            combo.Name = "combo";
            String[] positions = { "Striker", "Midfielder", "Defender", "Goalkeeper" };
            combo.Items.AddRange(positions);
            dataGridView1.Columns.Add(combo);
        }

    }
}

Cheers.

Comments