我有这段代码将Dictionary的值分配给一个组合框:
private void PopulateComboBoxWithSchedulableWeeks()
{
int WEEKS_TO_OFFER_COUNT = 13;
BindingSource bs = new BindingSource();
Dictionary<String, DateTime> schedulableWeeks =
GetWeekBeginningsDict(WEEKS_TO_OFFER_COUNT);
bs.DataSource = schedulableWeeks;
comboBoxWeekToSchedule.DataSource = bs;
comboBoxWeekToSchedule.DisplayMember = "Key";
comboBoxWeekToSchedule.ValueMember = "Value";
}
public static Dictionary<String, DateTime> GetWeekBeginningsDict(int
countOfWeeks)
{
DateTime today = DateTime.Today;
int daysUntilMonday = ((int)DayOfWeek.Monday - (int)today.DayOfWeek + 7)
% 7;
DateTime nextMonday = today.AddDays(daysUntilMonday);
Dictionary<String, DateTime> mondays = new Dictionary<String, DateTime>
();
if (!IsAssemblyOrConventionWeek(nextMonday))
{
mondays.Add(nextMonday.ToLongDateString(), nextMonday);
}
for (int i = 0; i < countOfWeeks; i++)
{
nextMonday = nextMonday.AddDays(7);
if (!IsAssemblyOrConventionWeek(nextMonday))
{
mondays.Add(nextMonday.ToLongDateString(), nextMonday);
}
}
return mondays;
}
在运行时,我得到了“无法绑定到新的显示成员。”,但是,在这一行:
comboBoxWeekToSchedule.ValueMember = "Value";
为什么?
答案 0 :(得分:4)
您无法将ComboBox
绑定到字段。
在定义DisplayMember和DisplayValue后尝试加载DataSource:
comboBoxWeekToSchedule.DisplayMember = "Key";
comboBoxWeekToSchedule.ValueMember = "Value";
comboBoxWeekToSchedule.DataSource = bs;
答案 1 :(得分:0)
增加你的组合框的大小,我打赌它说System.Collections.Generic.Dictionary...
,但我怀疑你知道答案From This Thread。你还有什么样的回声?