C# DataGridView - ImageColumn,ComboBoxColumn,CheckBoxColumn

| Page Views: 262

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.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section.

About Me.

After completing his Software Engineering bachelors program, Oclemy(Clement Ochieng) these days is a man of two lives. At day he works for a startup in Nairobi, Kenya. At night he works tirelessly on building ProgrammingWizards TV, a tv channel for student coders and this website to help share the source code. In between he practices Meditation and Self actualization to help him keep balance. He also likes going for long solo walks to connect more with nature.




Recommendations


What do You Think


Previous Post Next Post