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.
-
-
-
-
- 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;
- }
-
-
-
-
-
- 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;
- }
-
-
-
-
-
- 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;
- }
Step 3: Bind DataGridView with datatable.
- {
- dgv.DataSource = GetGridTable();
- }
Now after we have done this code section, if we run the application, it will look like this,
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)
- {
-
- DataGridViewComboBoxCell l_objGridDropbox = new DataGridViewComboBoxCell();
-
-
- if (dgv.Columns[e.ColumnIndex].Name.Contains("Description"))
- {
-
- dgv[e.ColumnIndex, e.RowIndex] = l_objGridDropbox;
- l_objGridDropbox.DataSource = GetDescriptionTable();
- 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";
- }
-
- }
-
- }
So if we run our application and click over Datagridview cell, this above code will attach the combo box with this clicked cell and it will look like this.
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.