简体   繁体   English

ComboBox DisplayMember、ValueMember 和 DataSource 方法

[英]ComboBox DisplayMember, ValueMember, and DataSource Method

I am working on ac# winforms program that has three comboboxes, each combobox has a different displaymember, valuemember, and datasource.我正在开发具有三个组合框的 ac# winforms 程序,每个组合框都有不同的显示成员、值成员和数据源。 I would like to try to create a method in which I can use to set the member values and datasource that works for all three comboboxes我想尝试创建一种方法,我可以在其中设置适用于所有三个组合框的成员值和数据源

here is what I am currently using for the comboboxes这是我目前用于组合框的内容

private void kitLoad (string kitDisplay, string kitValue, object kitSource)
{
     kits_comboBox.DisplayMember = kitDisplay;
     kits_comboBox.ValueMember = kitValue;
     kits_comboBox.DataSource = kitSource;
}
private void pspLoad(string pspDisplay, string pspValue, object pspSource)
{
     psp_comboBox.DisplayMember = pspDisplay;
     psp_comboBox.ValueMember = pspValue;
     psp_comboBox.DataSource = pspSource;
}
private void plateLoad(string plateDisplay, string plateValue, object plateSource)
{
     plates_comboBox.DisplayMember = plateDisplay;
     plates_comboBox.ValueMember = plateValue;
     plates_comboBox.DataSource = plateSource;
}

It does work, But I feel like I could compress all of that into one method, any help is greatly appreciated.它确实有效,但我觉得我可以将所有这些压缩到一个方法中,非常感谢任何帮助。

just pass your combo along只需传递您的组合

private void LoadCombo(ComboBox cbo, string disp, string val, object data)
{
     cbo.DisplayMember = disp;
     cbo.ValueMember = val;
     cbo.DataSource = data;
}

will work for any combo box适用于任何组合框

LoadCombo(kits_comboBox, "displayProperty", "valueProperty", data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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