我知道如何将查询中的值分配给 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";
我是一名优秀的程序员,十分优秀!