gpt4 book ai didi

c# - 如何将字典分配给组合框的项目(显示和值成员)?

转载 作者:太空宇宙 更新时间:2023-11-03 23:21:32 25 4
gpt4 key购买 nike

我知道如何将查询中的值分配给 ListBox 的 Display 和 Value 成员:

using (SqlConnection con = new SqlConnection(ReportRunnerConstsAndUtils.CPSConnStr))
{
using (SqlCommand cmd = new SqlCommand(ReportRunnerConstsAndUtils.SelectUnitsQuery, con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
((ListBox)checkedListBoxUnits).DataSource = dt;
((ListBox)checkedListBoxUnits).DisplayMember = "Unit";
((ListBox)checkedListBoxUnits).ValueMember = "Unit";
}
}
}

...以及如何像这样将单个值分配给 ComboBox:

List<String> schedulableWeeks = PlatypusUtils.GetWeekBeginnings(WEEKS_COUNT).ToList();
BindingSource bs = new BindingSource();
bs.DataSource = schedulableWeeks;
comboBoxWeekToSchedule.DataSource = bs;

...但是如何将字典分配给组合框,将我的字典的字符串值作为显示值,将日期时间作为值成员?我试过这个:

Dictionary<String, DateTime> schedulableWeeks = 
PlatypusUtils.GetWeekBeginningsDict(WEEKS_TO_OFFER_COUNT);
BindingSource bs = new BindingSource();
bs.DataSource = schedulableWeeks;
comboBoxWeekToSchedule.DataSource = bs;
comboBoxWeekToSchedule.DisplayMember = schedulableWeeks[0];
comboBoxWeekToSchedule.ValueMember = schedulableWeeks[1];

...认为我可以通过 Dictionary 元素 0 访问字符串,通过元素 1 访问 DateTime,但它甚至无法编译(“参数 1:无法从‘int’转换为‘string’”)-两行都使用相同的错误消息。

最佳答案

尝试如下设置组合框的键和值:

comboBoxWeekToSchedule.DisplayMember = "Key";
comboBoxWeekToSchedule.ValueMember = "Value";

关于c# - 如何将字典分配给组合框的项目(显示和值成员)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35260197/

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