如何将字典分配给组合框的项目(显示和值成员)?
How can I assign a Dictionary to a combobox's Items (Display and Value Members)?
我知道如何将查询中的值分配给 ListBox 的显示和值成员:
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,但它甚至不编译 ("Argument 1: cannot convert from 'int' to 'string'") - 两行的错误信息相同。
您尝试过使用 toDictionary()
吗?我不确定 PlatypusUtils.GetWeekBeginningsDict 到底是什么 returns,但我猜这可以解决问题。
Dictionary<String, DateTime> schedulableWeeks = PlatypusUtils.GetWeekBeginningsDict(WEEKS_TO_OFFER_COUNT).ToDictionary(x => x.Key);
也可能是 KeyValuePair 的绑定源问题:
comboBoxWeekToSchedule.DisplayMember = "Key";
comboBoxWeekToSchedule.ValueMember = "Value";
像这样的东西应该可以工作:
BindingSource bs = new BindingSource();
bs.DataSource = schedulableWeeks;
comboBoxWeekToSchedule.DataSource = bs;
comboBoxWeekToSchedule.DisplayMember = "Key";
comboBoxWeekToSchedule.ValueMember = "Value";
基本上,字典中的每个项目都有一个"Key"和一个"Value",因为字典中的每个项目都是一个KeyValuePair<string,DateTime>
尝试为组合框设置键和值,如下所示:
comboBoxWeekToSchedule.DisplayMember = "Key";
comboBoxWeekToSchedule.ValueMember = "Value";
我知道如何将查询中的值分配给 ListBox 的显示和值成员:
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,但它甚至不编译 ("Argument 1: cannot convert from 'int' to 'string'") - 两行的错误信息相同。
您尝试过使用 toDictionary()
吗?我不确定 PlatypusUtils.GetWeekBeginningsDict 到底是什么 returns,但我猜这可以解决问题。
Dictionary<String, DateTime> schedulableWeeks = PlatypusUtils.GetWeekBeginningsDict(WEEKS_TO_OFFER_COUNT).ToDictionary(x => x.Key);
也可能是 KeyValuePair 的绑定源问题:
comboBoxWeekToSchedule.DisplayMember = "Key";
comboBoxWeekToSchedule.ValueMember = "Value";
像这样的东西应该可以工作:
BindingSource bs = new BindingSource();
bs.DataSource = schedulableWeeks;
comboBoxWeekToSchedule.DataSource = bs;
comboBoxWeekToSchedule.DisplayMember = "Key";
comboBoxWeekToSchedule.ValueMember = "Value";
基本上,字典中的每个项目都有一个"Key"和一个"Value",因为字典中的每个项目都是一个KeyValuePair<string,DateTime>
尝试为组合框设置键和值,如下所示:
comboBoxWeekToSchedule.DisplayMember = "Key";
comboBoxWeekToSchedule.ValueMember = "Value";