【问题标题】:DataGridViewComboBox error vb.netDataGridViewComboBox 错误 vb.net
【发布时间】: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


【解决方案1】:

我看不到你的所有代码,但这是一个关于将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

结果

【讨论】:

  • 是的 Vland,我可以得到这个,但是当我点击 datagridview 内的不同行时,会引发异常。
  • @RahulKishore 这可能是因为您添加了具有错误组合框单元值的新行。
  • @RahulKishore 我添加了一些示例。在添加新行之前,您需要获取一个有效且现有的组合框对象
  • 目前仍然一无所获,但看着@你的帖子,我想我可能需要重写我添加组合框项目的方式。谢谢,我一定会尽快回复您。
  • 这只是一种实现方式...另一个不错的选择是将 BindingSource 添加到您的 combobox.datasource 中,然后在添加新行时从那里选择您的值。
猜你喜欢
  • 2012-04-06
  • 1970-01-01
  • 2011-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-14
相关资源
最近更新 更多