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?