Add Combo box in a cell of DataGridView at Run Time - C#.NET Windows Form

In DataGridView control of windows form we can set the column type "DataGridViewComboBoxColumn" to show combo box in the cells of the column. Sometimes we need to show only a single or few cells having combo box. For this we need to add Combo box to the required cell on run time. Here is the sample code to do the same -

//to get the correct cell get value of row and column indexs of the cell
 ColIndex = 1;
 RowIndex = 1;

 DataGridViewComboBoxCell ComboBoxCell = new DataGridViewComboBoxCell();
 ComboBoxCell.Items.AddRange("XYZ", "ABC", "PQR");
 ComboBoxCell.Value = "XYZ";
 datagridview1[ColIndex, RowIndex] = ComboBoxCell;

From the above code DataGirdCell at the location (1,1) will be converted to a "DataGridViewComboBoxCell" and combo box will be shown in the cell.

It might be possible that to dropdown the combo box multiple mouse clicks are required. To activate combo box on single click following steps are required -

1. Set ReadOnly property of the combobox cell to false
2. Set EditMode property of DataGridView to EditOnEnter
3. Create EditingControlShowing event and add code to drop down the combo box

Here is the sample code to drop down the combo box and activate it on single click -

private void datagridview1_EditingControlShowing(object sender,                    DataGridViewEditingControlShowingEventArgs e)
            ComboBox ctrl = e.Control as ComboBox;
            ctrl.Enter -= new EventHandler(ctrl_Enter);
            ctrl.Enter += new EventHandler(ctrl_Enter);

        void ctrl_Enter(object sender, EventArgs e)
            (sender as ComboBox).DroppedDown = true;


Men's and Women's T-shirts -

Men's and Women's T-shirts -