Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change cell's ComboBox style in DataGridViewComboBoxColumn

Cells in DataGridViewComboBoxColumn have ComboBoxStyle DropDownList. It means the user can only select values from the dropdown. The underlying control is ComboBox, so it can have style DropDown. How do I change the style of the underlying combo box in DataGridViewComboBoxColumn. Or, more general, can I have a column in DataGridView with dropdown where user can type?

like image 891
chgman Avatar asked Oct 14 '08 17:10

chgman


People also ask

How to change dropdownstyle of combobox column in datagridviewcomboboxcolumn?

A DataGridViewComboBoxColumn don't have a DropDownStyle property. You can't change the style of in DataGridView, it's limited to DropDownList style by default. In an EditingControlShowing event of DataGridView, you can change style to DropDown.

Why does my cell look like a combobox when I edit?

When a cell is in edit mode, it always looks like a ComboBox control. If the DisplayStyleForCurrentCellOnly property value is false, the DisplayStyle property affects all cells in the column; otherwise, the DisplayStyle property affects only the current cell.

How do I enable typing in a datagridviewcomboboxcell?

By default a DataGridViewComboBoxCell does not support typing into the cell. There are reasons though that typing into the combo box works well for your application. To enable this, two things have to be done. First the DropDownStyle property of the ComboBox editing control needs to be set to DropDown to enable typing in the combo box.

How do I enable typing in a combobox?

First the DropDownStyle property of the ComboBox editing control needs to be set to DropDown to enable typing in the combo box. The second thing that needs to be done is to ensure that the value that the user typed into the cell is added to the combo box items collection.


2 Answers

void dataGridView1_EditingControlShowing(object sender, 
    DataGridViewEditingControlShowingEventArgs e)
{
    if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))
    {
        DataGridViewComboBoxEditingControl cbo = 
            e.Control as DataGridViewComboBoxEditingControl;
        cbo.DropDownStyle = ComboBoxStyle.DropDown;
    }
}

Problem with combobox and databound datagridview

like image 133
Aleris Avatar answered Sep 20 '22 00:09

Aleris


Following solution works for me

private void dataGridView1_CellValidating(object sender, 
    DataGridViewCellValidatingEventArgs e) 
{
    if (e.ColumnIndex == Column1.Index) 
    {
        // Add the value to column's Items to pass validation
        if (!Column1.Items.Contains(e.FormattedValue.ToString())) 
        {
            Column1.Items.Add(e.FormattedValue);
            dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = 
                e.FormattedValue;
        }
    }
}

private void dataGridView1_EditingControlShowing(object sender, 
    DataGridViewEditingControlShowingEventArgs e) 
{
    if (dataGridView1.CurrentCell.ColumnIndex == Column1.Index) 
    {
        ComboBox cb = (ComboBox)e.Control;
        if (cb != null) 
        {
            cb.Items.Clear();
            // Customize content of the dropdown list
            cb.Items.AddRange(appropriateCollectionOfStrings);
            cb.DropDownStyle = ComboBoxStyle.DropDown;
        }
    }
}
like image 21
chgman Avatar answered Sep 19 '22 00:09

chgman