2009-09-16 72 views
2

我有一個DataGridView與兩個DataGridViewComboBoxColumns。我想要使​​用第一列中的選定項目以每行爲基礎觸發第二列中項目的重新填充。嘗試將DataSource重新綁定到DataGridViewComboBoxCell時出錯?

下面是我到目前爲止的代碼。 「addlInfoParentCat」標識第一列,currentRow.Cells.Item(1)是我想要重新填充的DataGridViewComboBoxCell。 ExtEventAdditionalInfoType是我定義的包含字符串/值對的類型。

Private Sub dgvAdditionalInfo_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvAdditionalInfo.CellValueChanged 
    Dim currentCell As DataGridViewCell 
    currentCell = Me.dgvAdditionalInfo.CurrentCell 
    If Not currentCell Is Nothing Then 
     If currentCell.OwningColumn.DataPropertyName = "addlInfoParentCat" Then 
      Dim parentTypeID As Integer = currentCell.Value 

      Dim currentRow As DataGridViewRow = Me.dgvAdditionalInfo.CurrentRow 
      Dim subtypeCell As DataGridViewComboBoxCell = currentRow.Cells.Item(1) 

      Dim theChildren As New List(Of ExtEventAdditionalInfoType) 

      theChildren = Custom_ExtEventAdditionalInfoType.GetChildrenOfThisParentOrderByTypeName(parentTypeID) 
      subtypeCell.DataSource = Nothing 
      subtypeCell.DataSource = theChildren 
      subtypeCell.DisplayMember = "ExtEventAdditionalInfoTypeDescr" 
      subtypeCell.ValueMember = "ID_ExtEventAdditionalInfoType" 
     End If 
    End If 
End Sub 

基本上我看到的是,綁定第一次效果很好。當我在第一列中選​​擇一個項目時,它會在第二列中正確填充項目。我可以將行添加到DataGridView並重復該過程。

當我試圖在第二列已被綁定後嘗試更改第一列項目時,問題就出現了。我收到了以下對話框的無盡串:

System.ArgumentException:DataGridViewComboBoxCell值無效。

任何想法爲什麼會發生這種情況?提前致謝!

更新 CodeByMoonlight的建議似乎工作。

我之前再結合清除的DataGridViewComboBoxCell值:

.... 

      subtypeCell.DataSource = Nothing 
      subtypeCell.Value = Nothing 'here's the change 
      subtypeCell.DataSource = theChildren 

.... 

回答

2

嗯,只要你remodify第一個組合的價值樣子,你無效,你用來填充第二綁定和數據源組合,導致所有的錯誤。

+0

感謝您的回答CodeByMoonlight。恐怕我沒有跟上。每當我改變第一列中的選擇時,我都會得到新的兒童列表,並將第二個組合重新綁定到這個新列表中。綁定無效,但我重置它,不是嗎?或者我沒有正確地重新綁定? – John 2009-09-16 01:10:58

+0

我認爲關鍵是綁定短暫無效,而不是你然後糾正它。我想如果你在加載新的數據源之前清除第二個組合的值,它應該沒問題。不過,我是從Opera Mini發佈的,所以我現在無法測試它。 – MartW 2009-09-16 02:05:25

+0

修復它。謝謝! – John 2009-09-16 16:17:02

相關問題