1

I'm quite lost in this error I'm getting. I have a Combobox that I added to my dgv, I am able to fill my combo-box with the values I want yet I keep getting an exception when I make a selection change on the dgv itself. Every time I choose a value from the combo box and then perform a selection change on the dgv the error that is thrown is : DataGridViewComboBoxCell is not valid . After this error is thrown, the value in the combo box is set to nothing.

This is my first time posting and I've done alot of research for the past two days and I can't seem to get anywhere. If you guys would like me to post my code I will do so. Thanks.

Edit: added my code:

cmbItem = New cboItem(dr.Item("strFolderName"), dr.Item("strFolderPath"), dr.Item("strEntryID"))

Dim dtmTmp As Date = oItem.ReceivedTime
dgvEmails.Rows.Insert(intEmailPosition, {False, dtmTmp.ToString("dd-MMM-yyyy hh:mm tt"), GetRecipientEmail(oItem), oItem.subject.ToString, cmbItem, oItem.conversationid.ToString, oItem.entryid.ToString, strFoundBy, oItem.body})

DirectCast(dgvEmails.Rows(intEmailPosition).Cells("cboFileTo"), DataGridViewComboBoxCell).Items.Add(cmbItem)
DirectCast(dgvEmails.Rows(intEmailPosition).Cells("cboFileTo"), DataGridViewComboBoxCell).DisplayMember = "DisplayText"

This is how I am adding the items into the combobox. Am I doing something wrong?

Edit: Added extra code

Dim cellComboBox As ComboBox = TryCast(e.Control, ComboBox)
        RemoveHandler cellComboBox.SelectedIndexChanged, AddressOf Me.cellComboBox_SelectedIndexChanged
        AddHandler cellComboBox.SelectedIndexChanged, AddressOf Me.cellComboBox_SelectedIndexChanged 'trapping the event handler


        If cellComboBox IsNot Nothing Then
            'load all values into the combox box 
            cellComboBox.MaxDropDownItems = 6 'drop down list can only have 5 items in there

            Try
                strEmail = dgvEmails.SelectedRows(0).Cells(2).Value.ToString 'cells(2) holds the email address
                strConvoID = dgvEmails.SelectedRows(0).Cells(5).Value.ToString 'cells(5) holds the conversation id of the email
            Catch ex As Exception
            End Try
            'call GetSuggestion function here
            objclsSuggesstion.GetSuggestion(strConvoID, strEmail, NUMBER_SUGGESTIONS)

            For intI = 0 To NUMBER_SUGGESTIONS - 1
                dr = objclsSuggesstion.GetItem(intI)
                If dr IsNot Nothing Then
                    'add dr to the combo box
                    cboItem = New cboItem(dr.Item("strFolderName"), dr.Item("strFolderPath"), dr.Item("strEntryID"))


                    'If Not cellComboBox.SelectedItem.FolderEntryID = cboItem.FolderEntryID Then 'if does not exist then add
                    cellComboBox.Items.Add(cboItem)
                    'End If

                Else
                    Exit For
                End If
            Next

            'cellComboBox.Items.Add(cboItem)
            cboItem = Nothing 'make object nothing here
            cboItem = New cboItem("", "", "", "<choose folder>...") 'create new object & add

            'if <choose folder>... doesn't exist, then you add it. 
            Try
                If Not cellComboBox.SelectedItem.DisplayText = cboItem.DisplayText Then
                    'cellComboBox.Items.Add(cboItem)
                    'DirectCast(dgvEmails.SelectedRows(0).Cells("cboFileTo"), DataGridViewComboBoxCell).Items.Add(cboItem)
                    'DirectCast(dgvEmails.SelectedRows(0).Cells("cboFileTo"), DataGridViewComboBoxCell).DisplayMember = "DisplayText"
                    cellComboBox.Items.Add(cboItem)
                End If
            Catch ex As Exception
            End Try

I hope this helps?

Rahul Kishore
  • 380
  • 1
  • 8
  • 19

1 Answers1

3

I can't see all your code, but this is an example about adding a list of objects of type cmbItem to a DataGridViewComboBoxColumn

Public Class Form1
Dim myItems As New List(Of cmbItem)

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    myItems.Add(New cmbItem() With {.Text = "yesterday", .DisplayText = Now.Date.AddDays(-1).ToString()})
    myItems.Add(New cmbItem() With {.Text = "now", .DisplayText = Now.Date.ToString()})
    myItems.Add(New cmbItem() With {.Text = "tomorrow", .DisplayText = Now.Date.AddDays(1).ToString()})


    'find combobox in datagridview, passing column index
    Dim ss = CType(DataGridView1.Columns(1), DataGridViewComboBoxColumn)

    'add my items to combobox
    For Each cmbItem As cmbItem In myItems
        ss.Items.Add(cmbItem)
    Next

    'set combobox properties
    ss.ValueMember = "Text"
    ss.DisplayMember = "DisplayText"
End Sub

End Class

Public Class cmbItem
    Property Text() As String
    Property DisplayText() As String
End Class

Result:

enter image description here

If you want to add a new row, you must be sure to add a valid combobox value in the comboboxColumn. In the following code...

Private Sub AddRow()
    DataGridView1.Rows.Add(New Object() {"New", myItems.First()})
    DataGridView1.Rows.Add(New Object() {"New", "12/01/1984"})
End Sub

The first row is added correctly, the second one gives and exception like yours "DataGridViewComboBoxCell value is not valid."

enter image description here

There are a lot of ways to add new rows getting a valid combobox item, here are some examples

Private Sub AddRow2()
    Dim ss = CType(DataGridView1.Columns(1), DataGridViewComboBoxColumn)

    'adding from my list
    DataGridView1.Rows.Add(New Object() {"New", myItems.First()})


    'adding from current combobox Items
    DataGridView1.Rows.Add(New Object() {"New", ss.Items.OfType(Of cmbItem).Last()})


    'querying from combobox added items
    Dim queryItem = (From i In ss.Items.OfType(Of cmbItem)() _
                    Where i.Text = "now" _
                    Select i).Single()

    DataGridView1.Rows.Add(New Object() {"New", queryItem})

End Sub

Result

enter image description here

Vland
  • 4,151
  • 2
  • 32
  • 43
  • Yes Vland, I am able to get this, but when I click on a different row inside of the datagridview, an exception is raised. – Rahul Kishore Jan 07 '14 at 19:43
  • @RahulKishore It's probably because you're adding that new row with a wrong comboboxcell value. – Vland Jan 07 '14 at 19:45
  • @RahulKishore i added some examples. You need to get a valid and existing combobox object before adding the new row – Vland Jan 07 '14 at 20:12
  • still not getting anywhere at the moment, but looking @ your post I think I may need to rewrite how I'm adding my combo box items. Thanks I will definitely get back to you soon. – Rahul Kishore Jan 07 '14 at 20:17
  • This is just a way to do it... another good choice is adding a BindingSource to your combobox.datasource and then choosing your value from there when adding a new row. – Vland Jan 07 '14 at 20:20
  • Forgot to mention I have multiple items I need to fill into the combobox, maybe this is the reason? Debugging still leads me to nothing. It seems on selection change, the items I added to the combo box are removed. – Rahul Kishore Jan 07 '14 at 22:40
  • @RahulKishore this might be the problem, combobox accept ONE value only. try assigning one and see if it works – Vland Jan 07 '14 at 22:43
  • Yes, no error occurs on the first item. Not even on selection change. – Rahul Kishore Jan 07 '14 at 22:45
  • @RahulKishore this solves the matter hopefully... you need other solutions if you want a multi-checked value. – Vland Jan 07 '14 at 22:47