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. namespace DataGridView
  4. {
  5. public partial class Form1 : Form
  6. {
  7. public Form1()
  8. {
  9. InitializeComponent();
  10. }
  11. private void btn_Click(object sender, EventArgs e)
  12. {
  13. DataGridViewComboBoxColumn dgvCmb = new DataGridViewComboBoxColumn();
  14. dgvCmb.HeaderText ="Name";
  15. dgvCmb.Items.Add("Ghanashyam");
  16. dgvCmb.Items.Add("Jignesh");
  17. dgvCmb.Items.Add("Ishver");
  18. dgvCmb.Items.Add("Anand");
  19. dgvCmb.Name="cmbName";
  20. myDataGridView.Columns.Add(dgvCmb);
  21. }
  22. }
  23. }

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

Hope this is clear to you guys.