Add ComboBox cell to DataGridview in C# on Cell Enter using DataGridViewComboBoxCell

We can add ComboBox to the datagridview cell dynamically on cell enter. That means whenever we focus on cell, we can display particular cell as combo box data to provide multiple options to the user to select.

 

Here we discuss about how to add ComboBox to the dataGridView cell by using DataGridViewComboBoxCell in C# with simple example. We will dynamically create the data source on cell enter and will bind it to the dataGridView cell.

 

First create some new windows application, then drag and drop dataGridView control to the win form. Now bind some data table as data source to the dataGridView as shown below.

 

private void Form1_Load(object sender, EventArgs e) 

{ 

            DataTable dt = new DataTable(); 

            dt.Columns.Add("Name", typeof(string)); 

            dt.Columns.Add("Company", typeof(string));

 

            for (int i = 0; i < 5; i++) 

            { 

                DataRow dr = dt.NewRow(); 

                dr["Name"] = "Name - " + i.ToString(); 

                dr["Company"] = "Company - " + i.ToString();

 

                dt.Rows.Add(dr); 

            }

 

            dataGridView1.DataSource = dt; 

}

 

 

Add dataGridview Cell Enter event to the datagridview1 and assign combox type cell DataGridViewComboBoxCell to the datagridview current selected cell. Here we are adding combBox cell the Company column of dataGridview and binding some company data table to the combBox as datasource as shown below.

 

using System;

using System.Data; 

using System.Windows.Forms;

 

namespace DataGridviewComboBoxColumnExample 

{ 

    public partial class Form1 : Form 

    { 

        delegate void SetComboBoxCellType(int iRowIndex); 

        bool bIsComboBox = false;

 

        public Form1() 

        { 

            InitializeComponent(); 

        }

 

        private void Form1_Load(object sender, EventArgs e) 

        { 

            DataTable dt = new DataTable(); 

 

            dt.Columns.Add("Name", typeof(string)); 

            dt.Columns.Add("Company", typeof(string)); 

 

            for (int i = 0; i < 5; i++) 

            { 

                DataRow dr = dt.NewRow(); 

                dr["Name"] = "Name - " + i.ToString(); 

                dr["Company"] = "Company - " + i.ToString();

                dt.Rows.Add(dr); 

            }

 

            dataGridView1.DataSource = dt; 

        } 

 

        private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e) 

        { 

            SetComboBoxCellType objChangeCellType = new SetComboBoxCellType(ChangeCellToComboBox);

 

            if (e.ColumnIndex == this.dataGridView1.Columns["Company"].Index) 

            { 

                this.dataGridView1.BeginInvoke(objChangeCellType, e.RowIndex); 

                bIsComboBox = false; 

            } 

        }

 

        private void ChangeCellToComboBox(int iRowIndex) 

        { 

            if (bIsComboBox == false) 

            { 

                DataGridViewComboBoxCell dgComboCell = new DataGridViewComboBoxCell(); 

                dgComboCell.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;

 

                DataTable dt = new DataTable();

 

                dt.Columns.Add("Name", typeof(string)); 

                dt.Columns.Add("Company", typeof(string));

 

                for (int i = 0; i < 5; i++) 

                { 

                    DataRow dr = dt.NewRow(); 

                    dr["Name"] = "Name - " + i.ToString(); 

                    dr["Company"] = "Company - " + i.ToString();

 

                    dt.Rows.Add(dr); 

                }

 

                dgComboCell.DataSource = dt; 

                dgComboCell.ValueMember = "Company"; 

                dgComboCell.DisplayMember = "Company";

 

                dataGridView1.Rows[iRowIndex].Cells[dataGridView1.CurrentCell.ColumnIndex] = dgComboCell; 

                bIsComboBox = true; 

            } 

        } 

    } 

}

 

We are invoking the ChangeCellToComboBox() method by using delegate in C#. Run the application and output display as shown below. Whenever you enter into the company cell, company field display as combo box. 

 

 

                                                                                               DataGridviewComboBoxColumnExample.zip (49.80 kb)