我完全迷失在這個錯誤中。我有一個組合框,我添加到我的dgv中,我可以用我想要的值填充我的組合框,但當我在dgv本身上進行選擇更改時,我不斷收到異常。每次我從組合框中選擇一個值,然後在dgv上執行選擇更改時,拋出的錯誤是:DataGridViewComboBoxCell無效。引發此錯誤後,組合框中的值將設置爲無。DataGridViewComboBox錯誤vb.net
這是我第一次發帖,我在過去兩天做了很多研究,我似乎無法獲得任何地方。如果你們想讓我發佈我的代碼,我會這樣做。謝謝。
編輯:加入我的代碼:
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"
這是我如何添加項目到組合框。難道我做錯了什麼?
編輯:增加了額外的代碼
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
我希望這可以幫助?
請粘貼代碼 – Vland
DisplayText屬性是我的對象內用於顯示文本的用戶屬性組合框的右側 –
瞭解發生了什麼並不容易。你添加了什麼樣的物體?你如何得到它們?是綁定到數據源的Combobox? – Vland