在窗体上绑定多个ComboBox C# [英] Bind multiple ComboBox on a form C#
本文介绍了在窗体上绑定多个ComboBox C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这似乎是一个简单的任务,但可能有一个更简单的方法。
This seems like a simple task but there may be an easier way.
我有一个包含30个组合框的表单,它们都需要相同的数据集。目前我绑定每一个:
I have a form with 30 comboboxes that all need the same data set. Currently I bind each one:
DataTable t = GetData();
this.ComboBox1.DataSource = t;
this.ComboBox1.DisplayMember = "heading";
this.ComboBox2.DataSource = t;
this.ComboBox2.DisplayMember = "heading";
this.ComboBox3.DataSource = t;
this.ComboBox3.DisplayMember = "heading";
...
...
this.ComboBoxN.DataSource = t;
this.ComboBoxN.DisplayMember = "heading";
有没有办法以更少的tedius方式绑定所有?
Is there a way to bind them all in a less tedius fashion?
感谢。
推荐答案
foreach (var control in this.Controls)
{
if (control is (ComboBox))
{
((ComboBox)control).DataSource = t;
((ComboBox)control).DisplayMember = "heading";
}
}
这篇关于在窗体上绑定多个ComboBox C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文