我的项目有3个组合框,它们都收集相似的数据(相同的选项,但它们会导致不同的用户选择) 我有一个由mysql数据填充的数据表 - 这个数据表链接到这些组合框中的每一个。不幸的是,当你在其中一个组合框中选择一个列表项时,它会在所有3个组合框中选择它(我不希望它这样做)
项目在加载时绑定到组合框,如下所示:
Private Sub ptnew_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox5.DataSource = insurancecompanies
ComboBox5.DisplayMember = "IName"
ComboBox5.ValueMember = "IID"
ComboBox7.DataSource = insurancecompanies
ComboBox7.DisplayMember = "IName"
ComboBox7.ValueMember = "IID"
ComboBox6.DataSource = insurancecompanies
ComboBox6.DisplayMember = "IName"
ComboBox6.ValueMember = "IID"
End Sub
如何阻止此操作发生
答案 0 :(得分:1)
克隆您的数据源,以便它们是独立的实体。
Private Sub ptnew_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox5.DataSource = insurancecompanies.Copy
ComboBox5.DisplayMember = "IName"
ComboBox5.ValueMember = "IID"
ComboBox7.DataSource = insurancecompanies.Copy
ComboBox7.DisplayMember = "IName"
ComboBox7.ValueMember = "IID"
ComboBox6.DataSource = insurancecompanies.Copy
ComboBox6.DisplayMember = "IName"
ComboBox6.ValueMember = "IID"
End Sub
修改:将Clone
方法更改为Copy
以保留数据