我有:
Dim ds As DataSet = New DataSet
Dim dt As DataTable = New DataTable
adapter.Fill(ds, "Table")
dt = ds.Tables("Table")
cmbx1.DataSource = dt
cmbx1.DisplayMember = "field1"
cmbx1.ValueMember = "field2"
cmbx2.DataSource = dt
cmbx2.DisplayMember = "field1"
cmbx2.ValueMember = "field2"
因此,当我从 cmbx1 中选择某些内容时,它也会在 cmbx2 中选择。这是为什么?
我是否需要 cmbx2 的 dt 副本才能获得相同的值,但可以独立选择?
最佳答案
这是因为它们引用了同一个对象。
使用dt.Copy()
解决问题。
A new DataTable with the same structure (table schemas and constraints) and data as this DataTable. If these classes have been derived, the copy will also be of the same derived classes. Both the Copy and the Clone methods create a new DataTable with the same structure as the original DataTable. The new DataTable created by the Copy method has the same set of DataRows as the original table, but the new DataTable created by the Clone method does not contain any DataRows.
关于vb.net - 一张表和两个组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4938551/