Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. DataGridView ComboBox: Allowing user to type in a value NOT in the Items collection.

DataGridView ComboBox: Allowing user to type in a value NOT in the Items collection.

Scheduled Pinned Locked Moved C#
helpquestionarchitecture
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    David Fleming
    wrote on last edited by
    #1

    This question has been asked, in one way or another, a few times and I've yet to see a solid answer. I've searched the articles and messages but don't see a solution. Here's my desire: A normal ComboBox, when the DropDownStyle is set to ComboBox rather than ComboBoxList, will allow the user to select an item from the Items list OR type in their own value. Near as I can tell, the DataGridView ComboBox performs validation to see if the Value matches an item in the list. If that Value is not found in the list, you get the error message: "System.ArgumentException: DataGridViewComboBoxCell value is not valid" Basically making the DataGridViewComboBoxCell function like a ComboBoxList -- in essence NOT allowing the user to type in a value that isn't in the list. Is there a way around this? Is there a way for a DataGridViewComboBoxCell to allow the user to EITHER select a Value from the list OR type in a new value that is NOT already in the list? I am definitely NOT the only one out here with this question / problem, so if you have any ideas on a way around it, PLEASE let us all know. Thanks a bunch.

    D P 2 Replies Last reply
    0
    • D David Fleming

      This question has been asked, in one way or another, a few times and I've yet to see a solid answer. I've searched the articles and messages but don't see a solution. Here's my desire: A normal ComboBox, when the DropDownStyle is set to ComboBox rather than ComboBoxList, will allow the user to select an item from the Items list OR type in their own value. Near as I can tell, the DataGridView ComboBox performs validation to see if the Value matches an item in the list. If that Value is not found in the list, you get the error message: "System.ArgumentException: DataGridViewComboBoxCell value is not valid" Basically making the DataGridViewComboBoxCell function like a ComboBoxList -- in essence NOT allowing the user to type in a value that isn't in the list. Is there a way around this? Is there a way for a DataGridViewComboBoxCell to allow the user to EITHER select a Value from the list OR type in a new value that is NOT already in the list? I am definitely NOT the only one out here with this question / problem, so if you have any ideas on a way around it, PLEASE let us all know. Thanks a bunch.

      D Offline
      D Offline
      David Fleming
      wrote on last edited by
      #2

      I found a little more info on this problem searching MS. Here's a comment:

      The DataGridViewComboBox must hold all possible values that it will display
      in it's items collection. For example, when binding to the Customers table
      and having the CompanyName field a combobox the combobox must contain all
      possible values. If the grid attempts to set the cell's value where the
      combobox cell doesn't have the value then we throw the dataError event.

      To workaround this, handle the DataError event of the DataGridView. For
      example:

      void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs
      e)
      {
      // ignore the data error occurring under the territoryComboBoxColumn
      if (e.ColumnIndex != territoryComboBoxColumn.Index)
      {
      MessageBox.Show("Data error occurs:" + e.Exception.Message);
      }
      }

      This tells us how to avoid the Exception. However, it doesn't address allowing the user's typed input to become the value for that cell. Any thoughts?

      P 1 Reply Last reply
      0
      • D David Fleming

        I found a little more info on this problem searching MS. Here's a comment:

        The DataGridViewComboBox must hold all possible values that it will display
        in it's items collection. For example, when binding to the Customers table
        and having the CompanyName field a combobox the combobox must contain all
        possible values. If the grid attempts to set the cell's value where the
        combobox cell doesn't have the value then we throw the dataError event.

        To workaround this, handle the DataError event of the DataGridView. For
        example:

        void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs
        e)
        {
        // ignore the data error occurring under the territoryComboBoxColumn
        if (e.ColumnIndex != territoryComboBoxColumn.Index)
        {
        MessageBox.Show("Data error occurs:" + e.Exception.Message);
        }
        }

        This tells us how to avoid the Exception. However, it doesn't address allowing the user's typed input to become the value for that cell. Any thoughts?

        P Offline
        P Offline
        Pedram Behroozi
        wrote on last edited by
        #3

        Did you try this?

        if (e.ColumnIndex != territoryComboBoxColumn.Index)
        {
        MessageBox.Show("Data error occurs:" + e.Exception.Message);
        //Add user's typed input into ComboBox here
        }

        When you're alone in the Dark, Fear will protect you...

        1 Reply Last reply
        0
        • D David Fleming

          This question has been asked, in one way or another, a few times and I've yet to see a solid answer. I've searched the articles and messages but don't see a solution. Here's my desire: A normal ComboBox, when the DropDownStyle is set to ComboBox rather than ComboBoxList, will allow the user to select an item from the Items list OR type in their own value. Near as I can tell, the DataGridView ComboBox performs validation to see if the Value matches an item in the list. If that Value is not found in the list, you get the error message: "System.ArgumentException: DataGridViewComboBoxCell value is not valid" Basically making the DataGridViewComboBoxCell function like a ComboBoxList -- in essence NOT allowing the user to type in a value that isn't in the list. Is there a way around this? Is there a way for a DataGridViewComboBoxCell to allow the user to EITHER select a Value from the list OR type in a new value that is NOT already in the list? I am definitely NOT the only one out here with this question / problem, so if you have any ideas on a way around it, PLEASE let us all know. Thanks a bunch.

          P Offline
          P Offline
          Patrick Etc
          wrote on last edited by
          #4

          The best (and pretty much only) way I have ever found to address this kind of problem is to hook into the EditingControlShowing event of the DataGridView. When you do, you can detect that it is a ComboBoxCell being shown, and that the control is a DataGridViewComboBoxEditingControl (or something to that effect). This control is a very simple child class of the normal ComboBox control with a few extra properties for databinding in a data grid. In the EditingControlShowing event, you can hook into the ComboBox as you normally would, and detect when its data changes and when it is closing. When it is closing, you can edit the data being bound to the ComboBox's available items list. One caveat, I've never tried this. It may be impossible - I don't know exactly what order the DataGridView would carry out this operation (control closes->you add new item to bound data list->DataGridView validates the input against this new list). It may be that the data is validated before it will change the list of data, in which case you're up a creek unless you can find some way to add new items as the user is typing. At any rate, if this is possible at all, you'll probably find some way to do it using this event. There may be another way, and I'm all ears if there is - these are the kinds of problems I love solving with the DGV :)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups