繁体   English   中英

DataGridViewComboBox错误vb.net

[英]DataGridViewComboBox error vb.net

我在这个错误中迷失了。 我有一个我添加到我的dgv的Combobox,我能够用我想要的值填充我的组合框但是当我在dgv本身进行选择更改时我仍然会遇到异常。 每次我从组合框中选择一个值,然后对dgv执行选择更改时抛出的错误是: DataGridViewComboBoxCell无效 抛出此错误后,组合框中的值将设置为空。

这是我第一次发帖,过去两天我做了很多研究,似乎无处可去。 如果你们希望我发布我的代码,我会这样做。 谢谢。

编辑:添加了我的代码:

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

我希望这有帮助?

我无法看到您的所有代码,但这是一个关于将类型为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

结果:

在此输入图像描述

如果要添加新行,必须确保在comboboxColumn中添加有效的组合框值。 在以下代码中......

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

第一行正确添加,第二行提供和您的“DataGridViewComboBoxCell值无效”之类的异常

在此输入图像描述

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

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

结果

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM