DataSourceをDataGridViewComboBoxCellに再バインドしようとしてエラーが発生しましたか?
-
07-07-2019 - |
質問
2つのDataGridViewComboBoxColumnsを持つDataGridViewがあります。最初の列で選択したアイテムを使用して、行ごとに2番目の列のアイテムの再入力をトリガーします。
これまでのコードは次のとおりです。 " 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
基本的に、最初にバインドがうまく機能することを確認します。最初の列で項目を選択すると、2番目の列に項目が正しく入力されます。 DataGridViewに行を追加して、プロセスを繰り返すことができます。
2列目が既にバインドされた後に1列目の項目を変更しようとすると問題が発生します。次のようなダイアログボックスの無限の文字列が表示されます。
System.ArgumentException:DataGridViewComboBoxCell値が無効です。
なぜこれが起こっているのか考えていますか?事前に感謝します!
更新 CodeByMoonlightの提案は機能しているようです。
再バインドする前にDataGridViewComboBoxCellの値をクリアします:
....
subtypeCell.DataSource = Nothing
subtypeCell.Value = Nothing 'here's the change
subtypeCell.DataSource = theChildren
....
解決
まあ、最初のコンボの値を変更するとすぐに、2番目のコンボの設定に使用したバインディングとデータソースが無効になり、すべてのエラーが発生するように見えます。
所属していません StackOverflow