How to create combobox cell with DisplayStyle ComboBoxList?

Robert picture Robert · Dec 15, 2011 · Viewed 7.2k times · Source

I have datagridview and column with cells DataGridViewComboboxCell. DisplayStyle is set as Nothing. When I start typing in this cell changes its style to combobox. And I cannot provide my tekst, because cell search item according to text providing by me. I want to change its style to comboboxlist as it is in combobox. I has empty field where I can type my text. It is possible to do this in combobox ?

Thanks

Answer

Enigma State picture Enigma State · Dec 15, 2011

you can try like this..

    public void ComboList1()
    {
        DataGridViewComboBoxColumn combo1 = new DataGridViewComboBoxColumn();
        combo1.HeaderText = "Country";
        combo1.Items.Add("Antarctica");
        combo1.Items.Add("Belgium");
        combo1.Items.Add("Canada");
        combo1.Items.Add("Finland");
        combo1.Items.Add("Albania");
        combo1.Items.Add("India");
        combo1.Items.Add("Barbados");
        dataGridView1.Columns.Add(combo1);
    } 
    public void ComboList2()
    {
        DataGridViewComboBoxColumn combo2 = new DataGridViewComboBoxColumn();
        combo2.HeaderText = "Types of Jobs";
        combo2.Items.Add("Accounting");
        combo2.Items.Add("HR");
        combo2.Items.Add("Finance");
        combo2.Items.Add("Transportation");
        combo2.Items.Add("Testing");
        dataGridView1.Columns.Add(combo2);
    }

Call both these methods from the Form Constructor.

Now Click on DataGridView and generate EditingControlShowing event and write the folllowing code in it:

        if (e.Control is DataGridViewComboBoxEditingControl)
        {
            ((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown;
            ((ComboBox)e.Control).AutoCompleteSource = AutoCompleteSource.ListItems;
            ((ComboBox)e.Control).AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
        }

or try like this

private string[] list;
private DataSet ds;

public frmMain()
{
  InitializeComponent();
  ds = this.GetDataSet("SELECT deptName from empDept", "empDept");
  list = new string[ds.Tables[0].Rows.Count];
  comboBox1.AutoCompleteCustomSource.AddRange(list);
  comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
  comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
}


private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
  if (e.Control is ComboBox)
  {
    ComboBox cb = e.Control as ComboBox;
    cb.DropDownStyle = ComboBoxStyle.DropDown;

  }
}