Add a ComboBox and CheckBox Into the DataGridView in C#

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

Create an object of DataGridViewComboBoxColumn.

  1. DataGridViewComboBoxColumn dgvCmb = new DataGridViewComboBoxColumn();  

Give the Header name to the DataGridViewComboboxColumn for the DataGridView.

  1. dgvCmb.HeaderText ="Name"; 

Now add the items into the ComboBox.

  1. dgvCmb.Items.Add("Ghanashyam");  
  2. dgvCmb.Items.Add("Jignesh");  
  3. dgvCmb.Items.Add("Ishver");  
  4. dgvCmb.Items.Add("Anand");   

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

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

The whole code :

  1. using System;  
  2. using System.Windows.Forms;   
  3.   
  4. namespace DataGridView  
  5. {  
  6.     public partial class Form1 : Form  
  7.     {  
  8.         public Form1()  
  9.         {  
  10.             InitializeComponent();  
  11.         }  
  12.   
  13.         private void btn_Click(object sender, EventArgs e)  
  14.        {  
  15.             DataGridViewComboBoxColumn dgvCmb = new DataGridViewComboBoxColumn();  
  16.             dgvCmb.HeaderText ="Name";  
  17.             dgvCmb.Items.Add("Ghanashyam");  
  18.             dgvCmb.Items.Add("Jignesh");  
  19.             dgvCmb.Items.Add("Ishver");  
  20.             dgvCmb.Items.Add("Anand");  
  21.             dgvCmb.Name="cmbName";  
  22.             myDataGridView.Columns.Add(dgvCmb);  
  23.         }  
  24.     }  
  25. } 

See the following image.

01.png

Code For Adding CheckBox Into the DataGridView at Runtime.

First of all create the object of the DataGridViewCheckBoxColumn.

  1. DataGridViewCheckBoxColumn dgvChb = new DataGridViewCheckBoxColumn(); 

Then set the header text for that CheckBox for DataGridView.

  1. dgvChb.HeaderText = "Pass"; 

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

  1. 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.

  1. 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".

  1. dgvChb.ThreeState = true; 

See the following image.

06_Three_State.png

Finally add that CheckBox into the DataGridView.

  1. myDataGridView.Columns.Add(dgvChb); 

See the whole code:

  1. using System;  
  2. using System.Windows.Forms;  
  3.   
  4. namespace DataGridView  
  5. {  
  6.     public partial class Form1 : Form  
  7.     {  
  8.         public Form1()  
  9.         {  
  10.             InitializeComponent();  
  11.         }  
  12.    
  13.         private void btn_Click(object sender, EventArgs e)  
  14.         {  
  15.             DataGridViewCheckBoxColumn dgvChb = new DataGridViewCheckBoxColumn();  
  16.             dgvChb.HeaderText = "Pass";  
  17.             dgvChb.Name = "chbPass";  
  18.             dgvChb.FlatStyle = FlatStyle.Standard;  
  19.             dgvChb.ThreeState = true;  
  20.             dgvChb.CellTemplate.Style.BackColor = System.Drawing.Color.LightBlue;  
  21.             myDataGridView.Columns.Add(dgvChb);  
  22.         }  
  23.     }  
  24. } 

Hope this is clear to you guys.