vb.net - 一张表和两个组合框

标签 vb.net combobox datatable datasource

我有:

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/

相关文章:

wpf - 如何强制WPF组合框显示其项目列表?

javascript - 无法使用 jquery.cookie 保留复选框值

javascript - DataTable.js 实现一个过滤器来按索引隐藏行

javascript - 如何根据另一个 TD 元素更改 JQuery Datatables TD 元素中的图标?

vb.net - 在 vb.net 中获取命令行参数的最佳方法是什么

c# - 用作参数时无法推断 Action 委托(delegate)的返回类型

.net - 扩展 ImageMagickNet

mysql - 如何动态更改 sqlstatement 中用于使用组合框检索数据的列名称

vb.net - 如何从 MemoryStream 创建磁盘 (VB.net)

c# - Silverlight Xaml 中的 ComboBox IsEnabled 绑定(bind)问题