gpt4 book ai didi

c# - 组合框上的数据绑定(bind)

转载 作者:行者123 更新时间:2023-11-30 14:41:40 25 4
gpt4 key购买 nike

我正在将项目添加到组合框中,如下所示:

   readonly Dictionary<string, string> _persons = new Dictionary<string, string>();
....
//<no123, Adam>, <no234, Jason> etc..
foreach (string key in _persons.Keys)
{
//Adding person code
cmbPaidBy.Items.Add(key);
}

我想通过显示字典中的值(即名称)使组合框更具可读性。但是我需要个人代码(no123 等)来根据用户输入从数据库中获取。

正确的做法是什么?如何将值和键绑定(bind)到组合框项目?

最佳答案

DisplayMember 和 ValueMember 属性为您准备就绪:

cmbPaidBy.DataSource = new BindingSource(_persons, null); 
cmbPaidBy.DisplayMember = "Value";
cmbPaidBy.ValueMember = "Key";

更深入 information here ,在 BindingSource Class

The BindingSource component serves many purposes. First, it simplifies binding controls on a form to data by providing currency management, change notification, and other services between Windows Forms controls and data source

关于c# - 组合框上的数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3870215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com