srinivas
srinivas

Reputation: 1411

combo box in datagrid

What is the difference between normal combobox and the combobox in datagrid?

I mean to say I'm not able to give my value in combobox(in datagrid) but I am able to give (type any value) in normal combos.

Upvotes: 0

Views: 1798

Answers (3)

Ben Griswold
Ben Griswold

Reputation: 18321

View the source and you will notice the GridView's combobox control's id isn't what you would expect. This is because it is running at server and it's id is generated using its naming container, etc. If you reference the .NET-generated id (the one you see in the source) you should be able to manipulate the GridView's combobox accordingly.

I'm guessing the "normal" combobox isn't running at server and its id doesn't change thus you can reference the control as expected.

Just a hunch, but I hope it helps

Upvotes: 0

abhilash
abhilash

Reputation: 5641

  • the "Normal" combobox or the System.Windows.Forms.ComboBox is a Windows Form user control which is used to display multiple values of which a user may choose ONE option. The ComboBox.DropDownStyle which is of type ComboBoxStyle Enumeration property defines the behaviour of the text portion of the combo box is editable or not.

  • "Data grid" combo box is System.Windows.Forms.DataGridViewComboBoxColumn class which Represents a column of DataGridViewComboBoxCell objects. This class exhibits the almost behavior of "Normal" combobox, but the class hierarchy (deriving from DataGridViewColumn) is such that it can be embedded into a Datagrid.

Upvotes: 0

danish
danish

Reputation: 5600

The combobox control has its dropdown style set as dropdown. Thus you can type in values in it. While the DataGridView's combobox column has the property set as DropDownList. This is the reason you cannot type in it. If you wish to type in it, you can do this using following code:

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

Apart from this, you will need to handle the validating event of DataGridViewComboBoxEditingControl to decide what to do when user types value in the combobox.

Upvotes: 1

Related Questions