Results 1 to 3 of 3

Thread: [RESOLVED] Problem with DataGridViewComboBox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2004
    Posts
    195

    Resolved [RESOLVED] Problem with DataGridViewComboBox

    I am using a DataGridViewComboBox in a DataGridView. When a user selects a radio button, I would like to clear all the items in the DataGridViewComboBox and repopulate with new values. I do this as follows:

    Code:
            Dim iIndex As Integer
            Dim dgcb As DataGridViewComboBoxCell
    
            For iIndex = 0 To dgvRecipe.RowCount - 1
    
                dgcb = dgvRecipe.Rows(iIndex).Cells("Measure")
                dgcb.Items.Clear()
                If radEnglish.Checked Then
                    dgcb.Items.AddRange(New String() {"t", "T", "Cup", "Quart", "Gallon", "oz", "lbs"})
                Else
                    dgcb.Items.AddRange(New String() {"mg", "g", "ml", "Litre"})
                End If
            Next iIndex
    It appears to do the job, but later while the control is refreshing i get the error:

    The following exception occured in the DataGridView:

    System.ArguementException: DataGridViewComboBoxCell value is not valid.

    To replace this default dialog please handle the DataError event.


    Any ideas?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Problem with DataGridViewComboBox

    Let's say that your RadioButton is intially unchecked. That means that your combo box contains "mg", "g", "ml" and "Litre". Let's say that the user selects "mg" in one particular cell. Now let's suppose that the user checks the RadioButton. Your code will clear out the existing combo box items and add "t", "T", "Cup", "Quart", "Gallon", "oz" and "lbs". One of your cells still contains the value "mg" but that value is not contained in the combo box item list. That's what the error message is telling you. Your cell contains an invalid value, i.e. one that is not listed in the combo box.

    If you're going to change the list of items in the combo box then you first need to clear all the cell values in that column because none of them will be valid afterwards.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2004
    Posts
    195

    Smile Re: Problem with DataGridViewComboBox

    Thanks!
    That was indeed the problem!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width