Soatl
Soatl

Reputation: 10592

C# DataGridviewComboBoxColumn doesnt do anything when clicked

I am very confused with what is happening here. I have a DataGridviewComboBoxColumn that I want to act like a combobox (clearly). I have the following code:

Code in designer.cs:

this.PurposeCol.DataPropertyName = "Purpose";
this.PurposeCol.DisplayStyle = System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox;
this.PurposeCol.HeaderText = "Purpose";
this.PurposeCol.Name = "PurposeCol";
this.PurposeCol.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.PurposeCol.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
this.PurposeCol.Width = 78;

Constructor on form page:

PurposeCol.ReadOnly = false;
PurposeCol.DataSource = tripPurposeComboBox.Items;  //Verified that this line fills the datasource with 14 items          
PurposeCol.DisplayMember = "ItemText";
PurposeCol.ValueMember = "ItemValue";

The problem is that when I click it nothing happens. The text displayed is what I am expecting, and I can confirm that there are 14 items in the DataSource, but I cannot seem to get any other items to display. Is there a special setting that needs to be set before DataGridviewComboBoxColumn to act like a ComboBox?

Upvotes: 0

Views: 220

Answers (2)

gulshanm01
gulshanm01

Reputation: 195

To use it as editable combobox, you need to implement validating event for combobox cell while editing control showing eg.

 private void datagrid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        if (datagrid.CurrentCell.ColumnIndex == 4)
        {
            if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))
            {
                DataGridViewComboBoxEditingControl cbo = e.Control as DataGridViewComboBoxEditingControl;
                cbo.DropDownStyle = ComboBoxStyle.DropDown;

                cbo.Validating += new CancelEventHandler(cbo_Validating);
                cbo.SelectedIndexChanged += new EventHandler(SpacingComBox_SelectedIndexChanged);
            }

        }
    }

and implement cbo_Validating and SpacingComBox_SelectedIndexChanged

void cbo_Validating(object sender, CancelEventArgs e) private void SpacingComBox_SelectedIndexChanged(object sender, EventArgs e)

Upvotes: 0

Grant Winney
Grant Winney

Reputation: 66449

You set the ReadOnly property to False for the column, but make sure the DataGridView.ReadOnly property is set to False too (it normally is by default).

dataGridView1.ReadOnly = false;

If it's set to True, it will override the ReadOnly property on your column, and you won't be able to open the drop-down.

Upvotes: 1

Related Questions