微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在C#窗体中绑定combobox

我有两个comboBox都使用相同的数据源。 每当我改变其中一个comboBox时,另一个改变为完全相同的值。

看来要解决这个问题,我需要使用数据绑定。

我已经尝试了很多东西,但似乎没有任何工作。 我的comboBox命名为comboBox1和comboBox2。 下面是我试图用来使comboBox2独立行事comboBox1的源代码。 任何帮助将不胜感激。

BindingSource bs = new BindingSource(this.claimTypeBindingSource,null); BindingContext bc = new BindingContext(); comboBox2.BindingContext = bc; comboBox2.DataSource = bs.DataSource; comboBox2.displayMember = "ClaimType";

编辑我刚刚拿了comboBox1的dataSource分配,并使用代码分配 – 代码现在看起来像这样。

绑定和x:用双向模式绑定问题

简单的CRUD使用WPF和数据绑定

数据绑定到Windows 8 ListView

DevExpress DataBinding,添加新logging

.NET字典键作为列表框数据源

private void Form1_Activated(object sender,EventArgs e) { BindingSource bs1 = new BindingSource(this.claimTypeBindingSource,null); BindingContext bc1 = new BindingContext(); comboBox1.BindingContext = bc1; comboBox1.DataSource = bs1.DataSource; comboBox1.displayMember = "ClaimType"; BindingSource bs2 = new BindingSource(this.claimTypeBindingSource,null); BindingContext bc2 = new BindingContext(); comboBox2.BindingContext = bc2; comboBox2.DataSource = bs2.DataSource; comboBox2.displayMember = "ClaimType"; }

这并没有解决问题。 如果我改变1 comboBox其他改变以及。

Windows-10 UWP绑定图像url到ListView中的图像源

ItemDataBound为Windows窗体DataGridView?


 不在Windows Phone中创build新行

XAML嵌套模板绑定

ComBox的选定值未更新

我刚刚解决了它:-)这工作 – 我把它放到我的真实项目,它完美的作品。

private void Form1_Activated(object sender,EventArgs e) { comboBox1.DataSource = new BindingSource(this.claimTypeBindingSource,null); comboBox1.displayMember = "ClaimType"; comboBox2.DataSource = new BindingSource(this.claimTypeBindingSource,null); comboBox2.displayMember = "ClaimType"; }

这是简单的代码,我用来填充两个组合框,并更改从第一个组合框的值不会更改其他的值。

Public Class Form1 Dim data As New List(Of Product) Private Sub Button1_Click(sender As Object,e As EventArgs) Handles Button1.Click Dim bs1 As New BindingSource(data,nothing) Dim bc1 As New BindingContext() ComboBox1.BindingContext = bc1 ComboBox1.DataSource = bs1.DataSource ComboBox1.displayMember = "ProductName" Dim bs2 As New BindingSource(data,nothing) Dim bc2 As New BindingContext() ComboBox2.BindingContext = bc2 ComboBox2.DataSource = bs2.DataSource ComboBox2.displayMember = "ProductName" End Sub Private Sub Form1_Load(sender As Object,e As EventArgs) Handles MyBase.Load For intCounter As Integer = 1 To 10 data.Add(New Product(intCounter,intCounter.ToString)) Next End Sub End Class Public Class Product Public Property ProductID As Integer = 0 Public Property ProductName As String = String.Empty Public Sub New(ID As Integer,Name As String) Me.ProductID = ID Me.ProductName = Name End Sub End Class

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐