Attach combo box inside Datagridview on CellClick of Datagridview.
Step 1: Add Window form and drag and drop Datagridview from tool box into form.

Step 2: Create data for binding Datagridview and combo boxes.
Suppose in our case, we need two combo boxes, “Description” and “PaidWith”
So we need three data tables, one for the “Description” combo box, the second for “PaidWith” combobox, and we need a third table for binding Datagridview.
- /// <summary>
- /// Get datatable of description.
- /// </summary>
- /// <returns></returns>
- private DataTable GetDescriptionTable()
- {
- DataTable l_dtDescription = new DataTable();
- l_dtDescription.Columns.Add("Description", typeof(string));
- l_dtDescription.Columns.Add("Type", typeof(string));
- l_dtDescription.Rows.Add("Lunch", "Expense");
- l_dtDescription.Rows.Add("Dinner", "Expense");
- l_dtDescription.Rows.Add("Breakfast", "Expense");
- l_dtDescription.Rows.Add("Designing", "Service");
- l_dtDescription.Rows.Add("Drawing", "Service");
- l_dtDescription.Rows.Add("Paper", "Material");
- l_dtDescription.Rows.Add("DrawingBoard", "Material");
- return l_dtDescription;
- }
- /// <summary>
- /// Get datatable of PaidWIth.
- /// </summary>
- /// <returns></returns>
- private DataTable GetPaidWithTable()
- {
- DataTable l_dtPaidwith = new DataTable();
- l_dtPaidwith.Columns.Add("PaidWith", typeof(string));
- l_dtPaidwith.Columns.Add("Code", typeof(string));
- l_dtPaidwith.Rows.Add("CreditCard", "CC");
- l_dtPaidwith.Rows.Add("DebitCard", "DC");
- return l_dtPaidwith;
- }
- /// <summary>
- /// Get the data for grid.
- /// </summary>
- /// <returns></returns>
- private DataTable GetGridTable()
- {
- DataTable l_dtGridTable = new DataTable();
- l_dtGridTable.Columns.Add("PaidWith", typeof(string));
- l_dtGridTable.Columns.Add("Description", typeof(string));
- l_dtGridTable.Rows.Add("CreditCard", "Drawing");
- return l_dtGridTable;
- }
- {
- dgv.DataSource = GetGridTable();
- }

Now we need combobox, when the user clicks over the cell of Datagridview.
Step 4: Add combo box in Datagridview cell on cellClick event.
- private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
- {
- if (e.ColumnIndex > -1)
- {
- // Bind grid cell with combobox and than bind combobox with datasource.
- DataGridViewComboBoxCell l_objGridDropbox = new DataGridViewComboBoxCell();
- // Check the column cell, in which it click.
- if (dgv.Columns[e.ColumnIndex].Name.Contains("Description"))
- {
- // On click of datagridview cell, attched combobox with this click cell of datagridview
- dgv[e.ColumnIndex, e.RowIndex] = l_objGridDropbox;
- l_objGridDropbox.DataSource = GetDescriptionTable(); // Bind combobox with datasource.
- l_objGridDropbox.ValueMember = "Description";
- l_objGridDropbox.DisplayMember = "Description";
- }
- if (dgv.Columns[e.ColumnIndex].Name.Contains("PaidWith"))
- {
- dgv[e.ColumnIndex, e.RowIndex] = l_objGridDropbox;
- l_objGridDropbox.DataSource = GetPaidWithTable();
- l_objGridDropbox.ValueMember = "PaidWith";
- l_objGridDropbox.DisplayMember = "PaidWith";
- }
- }
- }

Now in the next article we will learn,
- How to get selected values of combo box from datagridview.
- How over the datagridview cell the user needs to do three clicks for selecting value
One click will bring the combo box into cell, the second click will drop the list of combo boxes, and in the third click the user will be able to select the values but in the next article we will do it in two clicks.

Farid AbdiPosted Jan 19, 2021, 1:11 PM
Very good much better than the rubbish you tube videos that came up in Google
Vadivel MurthyPosted Oct 8, 2019, 6:10 AM
Excellent Explanation :)
SubashPosted Sep 11, 2016, 9:35 AM
Good one
Vignesh ManiPosted Jul 20, 2016, 11:15 AM
Nice
kalu singh raoPosted Jul 19, 2016, 7:59 AM
Nice...