DataGridViewComboBox: allow Value that is NOT in Items list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?RHJEQkY=?=

    DataGridViewComboBox: allow Value that is NOT in Items list

    I understand that the Value put into a DataGridViewCom boBoxCell has to be a
    member of the Items list or an exception is thrown.

    I also understand that you can override that exception by handling the
    DataError event and just doing nothing in the circumstance that caused the
    error.

    Here's my situation: I would like to allow the user to EITHER select an item
    from the ComboBox (in a DataGridView) OR type in a new value of their own.

    I've handled the EditingControlS howing event to change the DropDownStyle of
    the editing control to DropDown (rather than DropDownList). This allows me to
    type something new into the edit box of the ComboBox.

    Then I handled the CellEndEdit event to snag the Text from the editing
    ComboBox control. I take that Text and put it into the Value of the
    DataGridViewCom boBoxCell. This triggers the exception.

    If I override that exception, things move along OK, except that that cell
    now takes the value of the first item in the ComboBox Items list.

    Is there a way to force the ComboBox to accept the new value?

    Or perhaps a different way to work around this:
    Is there a way to have the DataGridView column be of the Text type, but
    attach a ComboBox instead of a TextBox for editing. Then take whatever the
    Text value is from the ComboBox EditingControl and put that into the
    DataGridViewTex tBoxCell?

    I hope: 1) this makes sense, and 2) someone out there knows how to pull this
    off.

    Thanks.
  • Ashutosh Bhawasinka

    #2
    Re: DataGridViewCom boBox: allow Value that is NOT in Items list

    Your combo box is using an data source, so you definitely have a
    table(or another source) associated with it. Jut add that value as new
    row in that table before the DataGridView validates the cell content.
    You can try to add the value in the table when the user has finished
    editing.
    Validation event is fired after the end-editing(or whatever) event is fired.

    Once you add the row in the underlying table, the validation will/should
    succeed.

    I have not tried this, but it should definitely work. I will try it out
    tomorrow.

    Thanks & Regards,
    Ashutosh Bhawasinka

    DrDBF wrote:
    I understand that the Value put into a DataGridViewCom boBoxCell has to be a
    member of the Items list or an exception is thrown.
    >
    I also understand that you can override that exception by handling the
    DataError event and just doing nothing in the circumstance that caused the
    error.
    >
    Here's my situation: I would like to allow the user to EITHER select an item
    from the ComboBox (in a DataGridView) OR type in a new value of their own.
    >
    I've handled the EditingControlS howing event to change the DropDownStyle of
    the editing control to DropDown (rather than DropDownList). This allows me to
    type something new into the edit box of the ComboBox.
    >
    Then I handled the CellEndEdit event to snag the Text from the editing
    ComboBox control. I take that Text and put it into the Value of the
    DataGridViewCom boBoxCell. This triggers the exception.
    >
    If I override that exception, things move along OK, except that that cell
    now takes the value of the first item in the ComboBox Items list.
    >
    Is there a way to force the ComboBox to accept the new value?
    >
    Or perhaps a different way to work around this:
    Is there a way to have the DataGridView column be of the Text type, but
    attach a ComboBox instead of a TextBox for editing. Then take whatever the
    Text value is from the ComboBox EditingControl and put that into the
    DataGridViewTex tBoxCell?
    >
    I hope: 1) this makes sense, and 2) someone out there knows how to pull this
    off.
    >
    Thanks.
    >

    Comment

    • =?Utf-8?B?RHJEQkY=?=

      #3
      Re: DataGridViewCom boBox: allow Value that is NOT in Items list

      Sorry, I forgot to mention that it is NOT actually data bound. I fill the
      Items list manually. I'll grant that I am using the results from a
      DataReader, but I manually loop through the DataReader
      while (reader.Read())
      { dgvCo.Items.Add (reader.GetStri ng(0)) }

      And while I think your solution would work, I don't actually want to save
      the Value that the user types. To be more specific on what I'm trying to
      accomplish.... The DataGridView is for entering an invoice. Most of the
      invoice items have a specific "name" but I want to possibility of a "Misc"
      invoice item into which the user can type whatever description of that
      invoice line item they want. I don't want or need this saved into the
      database of invoice items since it will vary with each invoice.

      "Ashutosh Bhawasinka" wrote:
      Your combo box is using an data source, so you definitely have a
      table(or another source) associated with it. Jut add that value as new
      row in that table before the DataGridView validates the cell content.
      You can try to add the value in the table when the user has finished
      editing.
      Validation event is fired after the end-editing(or whatever) event is fired.
      >
      Once you add the row in the underlying table, the validation will/should
      succeed.
      >
      I have not tried this, but it should definitely work. I will try it out
      tomorrow.
      >
      Thanks & Regards,
      Ashutosh Bhawasinka
      >

      Comment

      Working...