locked
Editable Combobox in a datagridview RRS feed

  • Question

  • I'm a little new to this .net stuff so I'm sure this question will be pretty easy.

    I'm trying to make a combobox editable in a datagridview.  Can anyone lead me down the right path?

    thx
    Tuesday, November 22, 2005 7:07 AM

Answers

  • What the code is doing is changing the combobox DropDownStyle to allow for the user to enter text while the combobox is in edit mode. Unfortunately, the standard DataGridView does not allow this to happen at Design-Time so you have to pull some tricks. When the field loses focus and the edit control is no longer available, the combobox reverts its style. Therefore, the validating event is being used to add values into the list of available values so that the items can still be displayed correctly.

    I threw the code from that previous post into a test application and it performed as expected with some quick testing. I know it seems like a pain (because it is, there should be a better way to do this, but...), but I think it will get you where you want to be. Give it a shot.

    Tuesday, November 22, 2005 8:26 PM

All replies

  • If you right click the grid at design time and select Edit Columns, the Column Editor will appear. Select the column that you want to have appear as a combobox and change the ColumnType property to DataGridViewComboBoxColumn.
    Tuesday, November 22, 2005 1:38 PM
  • That's right, i've done that.  How do you make the combobox editable now?

    Tuesday, November 22, 2005 5:18 PM
  • I think I'm misunderstanding what you mean by editable. Could you please explain further?
    Tuesday, November 22, 2005 7:14 PM
  • Took me a minute, but I think I know what you are wanting to do now. You want the user to be able to enter their own values in addition to those listed. Rather than repeating the solution, I'll just point you to another thread that answers your question:

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=13855&SiteID=1
    Tuesday, November 22, 2005 7:34 PM
  • Sorry about that, I should have clarified a bit more.

    This is the code generated by the VS Designer:

    dataGridViewCellStyle4.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;

    this.QTYUnits.DefaultCellStyle = dataGridViewCellStyle4;

    this.QTYUnits.DisplayStyle = System.Windows.Forms.DataGridViewComboBoxDisplayStyle.Nothing;

    this.QTYUnits.HeaderText = "QTY Units";

    this.QTYUnits.Items.AddRange(new object[] {

    "KM",

    "M",

    "DM",

    "CM"});

    this.QTYUnits.Name = "QTYUnits";

    this.QTYUnits.Width = 75;

    What I want to do, for example, is allow the user to enter their own units. Will the link above apply?

    Tuesday, November 22, 2005 7:47 PM
  • What the code is doing is changing the combobox DropDownStyle to allow for the user to enter text while the combobox is in edit mode. Unfortunately, the standard DataGridView does not allow this to happen at Design-Time so you have to pull some tricks. When the field loses focus and the edit control is no longer available, the combobox reverts its style. Therefore, the validating event is being used to add values into the list of available values so that the items can still be displayed correctly.

    I threw the code from that previous post into a test application and it performed as expected with some quick testing. I know it seems like a pain (because it is, there should be a better way to do this, but...), but I think it will get you where you want to be. Give it a shot.

    Tuesday, November 22, 2005 8:26 PM
  • Great, thx a lot.  I'll give it a try and let you know.
    Tuesday, November 22, 2005 8:55 PM
  • And what is the answer??? I need to be able to edit a DataGridViewComboBoxColumn but it doesn't have the property dropdownstyle I've reading some post out of this forums like "How do I enable typing in the combo box cell?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. 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. This is due to the requirement described in 3.5.1 section that a combo box cells value must be in the items collection or else a DataError event is raised. The appropriate place to add the value to the items collection is in the CellValidating event handler." but the datagridviewcombobox doesn't have that property. What can I do??
    Friday, August 4, 2006 2:27 PM
  • Here's my solution

    add an unboud combobox column next to the column you'd like to have editable
    set the width to 25 so you only see the drop down arrow
    set the DropDownWidth to 100, or whathever you might need

    on cellValidating event, just add
    DataGridView.CurrentRow.Cells(TextBoxColumnName).Value = DataGridView.CurrentRow.Cells(ComboBoxColumnName).Value

    your column, being Textbox is editable
    your column is still binded to your database

    you can pick a value from the unbound combobox beside, it just modifies the textbox value

    hope this helps
    Thursday, November 29, 2007 9:05 PM
  • Estupenda Solución Gracias!
    Friday, January 22, 2010 9:23 AM