Even Even - 1 year ago 137
C# Question

how to allow user manual entry in datagridview combobox in c#

I am trying to enter values in a datagridview Combobox. but it does not Allows. What to do?

Answer Source
private void GridStockItemEntry_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

        DataGridViewRow row = GridStockItemEntry.CurrentRow;
        DataGridViewCell cell = GridStockItemEntry.CurrentCell;
        if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))

            if (cell == row.Cells["ItemName"] && Convert.ToString(row.Cells["Type"].Value) == "Raw Material")
                DataGridViewComboBoxEditingControl cbo = e.Control as DataGridViewComboBoxEditingControl;

                cbo.DropDownStyle = ComboBoxStyle.DropDown;

                cbo.Validating += new CancelEventHandler(cbo_Validating);

    void cbo_Validating(object sender, CancelEventArgs e)

        DataGridViewComboBoxEditingControl cbo = sender as DataGridViewComboBoxEditingControl;

        DataGridView grid = cbo.EditingControlDataGridView;

        object value = cbo.Text;

        // Add value to list if not there

        if (cbo.Items.IndexOf(value) == -1)

            DataGridViewComboBoxCell cboCol = (DataGridViewComboBoxCell)grid.CurrentCell;

            // Must add to both the current combobox as well as the template, to avoid duplicate entries...



            grid.CurrentCell.Value = value;


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download