ARTICLE

Add a ComboBox and CheckBox Into the DataGridView in C#

Posted by Ghanashyam Nayak Articles | WebForms Controls February 18, 2012
In this article you will know how to add a ComboBox & CheckBox into the DataGridView at runtime.
Reader Level:
Download Files:
 

In this article you will know how to add a ComboBox & CheckBox into the DataGridView at runtime.

Create an object of DataGridViewComboBoxColumn.

DataGridViewComboBoxColumn dgvCmb = new DataGridViewComboBoxColumn(); 

Give the Header name to the DataGridViewComboboxColumn for the DataGridView.

dgvCmb.HeaderText ="Name";

Now add the items into the ComboBox.

dgvCmb.Items.Add("Ghanashyam");
dgvCmb.Items.Add("Jignesh");
dgvCmb.Items.Add("Ishver");
dgvCmb.Items.Add("Anand");

Give the name to that ComboBox to refer to it from the DataGridView.

dgvCmb.Name="cmbName";
Finally add that newly created ComboBox into the DataGridView.
myDataGridView.Columns.Add(dgvCmb);

The whole code :

using System;
using System.Windows.Forms; 

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

        private void btn_Click(object sender, EventArgs e)
       {
            DataGridViewComboBoxColumn dgvCmb = new DataGridViewComboBoxColumn();
            dgvCmb.HeaderText ="Name";
            dgvCmb.Items.Add("Ghanashyam");
            dgvCmb.Items.Add("Jignesh");
            dgvCmb.Items.Add("Ishver");
            dgvCmb.Items.Add("Anand");
            dgvCmb.Name="cmbName";
            myDataGridView.Columns.Add(dgvCmb);
        }
    }
}

See the following image.

01.png

Code For Adding CheckBox Into the DataGridView at Runtime.

 First of all create the object of the DataGridViewCheckBoxColumn.

DataGridViewCheckBoxColumn dgvChb = new DataGridViewCheckBoxColumn();

Then set the header text for that CheckBox for DataGridView.

dgvChb.HeaderText = "Pass";

Then set the name of that CheckBox for use in the DataGridView.

dgvChb.Name = "chbPass";

You can set the flat style of the CheckBox for the style the CheckBox will be displayed. The default flat style is standard.

dgvChb.FlatStyle = FlatStyle.Standard; 

There are four different flat styles. See the following images:

1) FlatStyle is Flat.

02_chbFlat.png

2) FlatStyle is Popup.

03_popup.png

3) FlatStyle is Stsndard.

04_Standard.png

4) FlatStyle is System.

05_System.png

You can also set the ThreeState CheckBox. In that there are a total of three kinds of states available & the user can set any one of them.

Assume that there are three states such as "Pass", "Fail" & "Backlog".

dgvChb.ThreeState = true;

See the following image.

06_Three_State.png

Finally add that CheckBox into the DataGridView.

myDataGridView.Columns.Add(dgvChb);

See the whole code:

using System;
using System.Windows.Forms;

namespace DataGridView
{
    public partial class Form1 :
Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void btn_Click(object sender, EventArgs e)
        {
            DataGridViewCheckBoxColumn dgvChb = new DataGridViewCheckBoxColumn();
            dgvChb.HeaderText = "Pass";
            dgvChb.Name = "chbPass";
            dgvChb.FlatStyle = FlatStyle.Standard;
            dgvChb.ThreeState = true;
            dgvChb.CellTemplate.Style.BackColor = System.Drawing.Color.LightBlue;
            myDataGridView.Columns.Add(dgvChb);
        }
    }
}

Hope this is clear to you guys.

COMMENT USING