B. *_*non 2 c# combobox dictionary bindingsource
我有这个代码将字典的值分配给组合框:
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;
}
Run Code Online (Sandbox Code Playgroud)
在运行时,我得到“无法绑定到新的显示成员。 ”但是,在此行中使用此代码:
comboBoxWeekToSchedule.ValueMember = "Value";
Run Code Online (Sandbox Code Playgroud)
为什么?
小智 6
您不能绑定ComboBox到字段。在定义 DisplayMember 和 DisplayValue 后尝试加载数据源:
comboBoxWeekToSchedule.DisplayMember = "Key";
comboBoxWeekToSchedule.ValueMember = "Value";
comboBoxWeekToSchedule.DataSource = bs;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3713 次 |
| 最近记录: |