【发布时间】:2014-01-07 19:18:32
【问题描述】:
我很迷失在我遇到的这个错误中。我有一个添加到我的 dgv 的组合框,我可以用我想要的值填充我的组合框,但是当我对 dgv 本身进行选择更改时,我不断收到异常。每次我从组合框中选择一个值,然后在 dgv 上执行选择更改时,抛出的错误是:DataGridViewComboBoxCell is not valid。引发此错误后,组合框中的值设置为空。
这是我第一次发帖,过去两天我做了很多研究,但我似乎无处可去。如果你们希望我发布我的代码,我会这样做。谢谢。
编辑:添加我的代码:
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
我希望这会有所帮助吗?
【问题讨论】:
-
请粘贴您的代码
-
DisplayText 属性是我的对象内的用户属性,用于显示组合框内的文本
-
很难理解发生了什么。你要添加什么样的对象?你怎么得到它们? Combobox 是否绑定到数据源?
-
对不起,我不清楚,这是我添加的用户创建的对象,它有 4 个属性,我在添加到组合框之前循环制作对象。组合框未绑定到数据源 Vland。
-
请在添加 DataGridViewComboBoxColumn 的位置发布代码。
标签: vb.net datagridview datagridviewcombobox