How to add a ComboBox to DatagridView programmatically in C#.Net


DatagridView in C# 2015 is an important tool to show database values. This control has different usage such as regular Grid. Here we explore the different approaches to the datagrid programing which will explaining how to add advanced combo, auto suggestion box etc to DataGridView control.

Easy steps we require

Start a new Windows Form project in your Visual Studio and Drop a DatagridView control to your project, our project require a DataGridViewComboBoxColumn object. Here we goes
  1. Initialize column header with appropriate text
  2. Create a DataGridViewComboBoxColumn object and fill with appropriate values, name and a header text.
  3. Add the DataGridViewComboBoxColumn object to the DatagridView

That’s the plan, just the drop the following code to the form’s load event method.

The C# code

            dataGridView1.ColumnCount = 1;
            dataGridView1.Columns[0].Name = “Slno”;
            DataGridViewComboBoxColumn dc = new DataGridViewComboBoxColumn();
            dc.HeaderText = “Order Items”;
            dc.Name = “Items”;
            dc.Items.Add(“Mango”);
            dc.Items.Add(“Carrot”);
            dc.Items.Add(“Sugar cane”);
            dc.Items.Add(“Potatto”);
            dc.Items.Add(“Grapes”);
            dc.Items.Add(“Chillie”);
            dc.MaxDropDownItems = 1;
            dataGridView1.Columns.Add(dc);
            dataGridView1.Columns.Add(“Qty”, “Qty”);

Lets run, and that’s all you need to know. Next time we will meet with Autocomplete Box poem.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.