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 somet开发者_JS百科hing 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.
精彩评论