简体   繁体   中英

one table and two comboboxes

I have:

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"

So, when I select something from cmbx1 it is also selected in cmbx2. Why is this?

Do I need a copy of dt for cmbx2 to get the same values, but be selected independently?

It's because they have a reference to the same object.

Use dt.Copy() to solve the problem.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM