DataGridViewComboBox错误vb.net

DataGridViewComboBox错误vb.net,vb.net,datagridview,datagridviewcombobox,Vb.net,Datagridview,Datagridviewcombobox,我在这个错误中迷失了方向。我有一个组合框,我添加到我的dgv中,我可以用我想要的值填充我的组合框,但是当我对dgv本身进行选择更改时,我会不断得到一个异常。每次我从组合框中选择一个值,然后对dgv执行选择更改时,抛出的错误是:DataGridViewComboxCell无效。抛出此错误后,组合框中的值设置为nothing 这是我第一次发帖,在过去的两天里我做了大量的研究,但我似乎什么也做不到。如果你们想让我发布我的代码,我会这样做。谢谢 编辑:添加了我的代码: cmbItem = New cbo

我在这个错误中迷失了方向。我有一个组合框,我添加到我的dgv中,我可以用我想要的值填充我的组合框,但是当我对dgv本身进行选择更改时,我会不断得到一个异常。每次我从组合框中选择一个值,然后对dgv执行选择更改时,抛出的错误是:DataGridViewComboxCell无效。抛出此错误后,组合框中的值设置为nothing

这是我第一次发帖,在过去的两天里我做了大量的研究,但我似乎什么也做不到。如果你们想让我发布我的代码,我会这样做。谢谢

编辑:添加了我的代码:

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
Dim cellComboBox As ComboBox=TryCast(例如控件,ComboBox)
RemoveHandler cellComboBox.SelectedIndexChanged,地址Me.cellComboBox\U SelectedIndexChanged
AddHandler cellComboBox.SelectedIndexChanged,AddressOf Me.cellComboBox\u SelectedIndexChanged'捕获事件处理程序
如果cellComboBox不是空的,那么
'将所有值加载到组合框中
cellComboBox.MaxDropDownItems=6'下拉列表中只能有5项
尝试
strEmail=dgvEmails.SelectedRows(0).单元格(2).Value.ToString'单元格(2)保存电子邮件地址
strConvoID=dgvEmails.SelectedRows(0).Cells(5).Value.ToString'单元格(5)保存电子邮件的对话id
特例
结束尝试
'在此处调用GetSuggestion函数
GetSuggestion.GetSuggestion(strConvoID、strEmail、NUMBER_建议)
对于intI=0,建议数量为-1
dr=objclsSuggesstion.GetItem(intI)
如果dr不是什么都不是
'将dr添加到组合框中
cboItem=新的cboItem(dr.Item(“strFolderName”)、dr.Item(“strFolderPath”)、dr.Item(“strEntryID”))
'如果不存在cellComboBox.SelectedItem.FolderEntryID=cboItem.FolderEntryID,则'如果不存在,则添加
cellComboBox.Items.Add(cboItem)
"完"
其他的
退出
如果结束
下一个
'cellComboBox.Items.Add(cboItem)
cboItem=Nothing'在此处使对象无效
cboItem=New cboItem(“,”,“,”,“…”)'创建新对象并添加
“如果。。。不存在,然后你添加它。
尝试
如果不是cellComboBox.SelectedItem.DisplayText=cboItem.DisplayText,则
'cellComboBox.Items.Add(cboItem)
'DirectCast(dgvEmails.SelectedRows(0).Cells(“cboFileTo”)、DataGridViewComboxCell.Items.Add(cboItem)
'DirectCast(dgvEmails.SelectedRows(0).Cells(“cboFileTo”),DataGridViewComboxCell.DisplayMember=“DisplayText”
cellComboBox.Items.Add(cboItem)
如果结束
特例
结束尝试

我希望这有帮助

我看不到您的所有代码,但这是一个将
cmbItem
类型的对象列表添加到
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
结果:

如果要添加新行,必须确保在combobox列中添加有效的combobox值。在下面的代码中

Private Sub AddRow()
    DataGridView1.Rows.Add(New Object() {"New", myItems.First()})
    DataGridView1.Rows.Add(New Object() {"New", "12/01/1984"})
End Sub
第一行添加正确,第二行给出了与您类似的异常“DataGridViewComboxCell值无效。”

有很多方法可以添加新行以获取有效的组合框项,下面是一些示例

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
结果


请粘贴您的codeDisplayText属性是“我的对象”中的一个用户属性,用于在组合框中显示文本。不容易理解发生了什么。您正在添加什么类型的对象?你怎么弄到的?组合框是否绑定到数据源?抱歉,我不清楚,我正在添加的是一个具有4个属性的用户创建的对象,在添加到组合框之前,我正在循环中创建该对象。组合框未绑定到数据源VLAN。请在添加DataGridViewComboBoxColumn的位置发布代码。是的,VLAN,我可以获得此信息,但当我单击datagridview中的另一行时,出现异常。@RahulKishore这可能是因为您使用错误的comboboxcell值添加新行。@RahulKishore我添加了一些示例。在添加新行之前,您需要获得一个有效且现有的combobox对象,但目前仍然没有任何进展,但是查看@your post我想我可能需要重写如何添加我的combobox项。谢谢,我一定会很快回复你的。这只是一种方法。。。另一个不错的选择是向combobox.datasource添加BindingSource,然后在添加新行时从中选择值。