2014-01-07 41 views
1

我完全迷失在這個錯誤中。我有一個組合框,我添加到我的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 

我希望這可以幫助?

+0

請粘貼代碼 – Vland

+0

DisplayText屬性是我的對象內用於顯示文本的用戶屬性組合框的右側 –

+0

瞭解發生了什麼並不容易。你添加了什麼樣的物體?你如何得到它們?是綁定到數據源的Combobox? – Vland

回答

2

我不能看到所有的代碼,但是這是有關添加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 

結果的一個例子:

enter image description here

如果你想添加一個新行,你必須確保在comboboxColumn中添加一個有效的組合框值。在下面的代碼...

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

第一行正確添加,第二個給出了和像你這樣的例外「的DataGridViewComboBoxCell值是無效的。」

enter image description here

有很多的方式來添加得到一個有效的組合框項新行,這裏有一些例子

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 

結果

enter image description here

+0

是的Vland,我能夠得到這個,但是當我點擊datagridview內的不同行時,會引發異常。 –

+0

@RahulKishore這可能是因爲你添加了一個錯誤的組合框的值。 – Vland

+0

@RahulKishore我加了一些例子。您需要在添加新行之前獲取有效的和現有的組合框對象 – Vland