在VB.Net中将数据绑定到ComboBox

时间:2009-06-22 11:13:38

标签: vb.net winforms

我正在尝试将CheckedListbox的Cheched项绑定到Combobox,我的代码如下所示

Private Sub chklColumns_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chklColumns.SelectedIndexChanged        
            cmbSort.DataSource = chklColumns.CheckedItems
End Sub

每当我第一次检查项目时,它会将一个项目加载到Combobox,但是当我选择第二个项目时,我仍然在组合中有单个项目... 我在上面的代码中做错了吗?

1 个答案:

答案 0 :(得分:0)

您应该在设置新值之前将DataSource设置为Nothing来重置绑定:

Private Sub chklColumns_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chklColumns.SelectedIndexChanged        
    cmbSort.DataSource = Nothing
    cmbSort.DataSource = chklColumns.CheckedItems
End Sub