将通用List 绑定到ComboBox
我有一个ComboBox,我想绑定一个通用的List。 任何人都可以看到为什么下面的代码不起作用? 绑定源中包含数据,但它不会填充ComboBox数据源。
FillCbxProject(DownloadData Down) { BindingSource bindingSource = new BindingSource(); bindingSource.DataSource = Down.ProjectList; cbxProjectd.DataSource = bindingSource; }
旁注:传递一个类的实例是不是很糟糕?
谢谢!
你需要调用Bind方法:
cbxProjectd.DataBind();
如果这是针对winforms那么你需要确保你所拥有的是什么,以下是有效的:
BindingSource bs = new BindingSource(); bs.DataSource = new List { "test1", "test2" }; comboBox1.DataSource = bs;
虽然您可以直接使用列表设置ComboBox的DataSource。
这是简单的方法(它正常工作):
List my_list = new List (); my_list.Add("item 1"); my_list.Add("item 2"); my_list.Add("item 3"); my_list.Add("item 4"); my_list.Add("item 5"); comboBox1.DataSource = my_list;
BindingSource bs = new BindingSource(); bs.DataSource = getprojectname(); comboBox1 = new ComboBox(); comboBox1.DataSource = bs;
这是一种不使用BindingSource的简单方法:
首先,添加字符串的通用列表,也许添加到“consts / utils”类:
public static List Months = new List { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
以下是将这些字符串添加到combobox的方法:
comboBoxMonth.Items.AddRange(UsageRptConstsAndUtils.Months.ToArray
使用上面的Yuriy Faktorovich的代码作为基础,这里是如何以给定的周数获得LongDateString格式的日期列表,并将它们分配给combobox。 这使用“星期一”但您可以简单地将“星期一”替换为任何其他DOW以满足您的目的:
private void PopulateSchedulableWeeks() { int WEEKS_COUNT = 13; List schedulableWeeks = PlatypusUtils.GetWeekBeginnings(WEEKS_COUNT).ToList(); BindingSource bs = new BindingSource(); bs.DataSource = schedulableWeeks; comboBoxWeekToSchedule.DataSource = bs; } public static List GetWeekBeginnings(int countOfWeeks) { // from http://stackoverflow.com/questions/6346119/datetime-get-next-tuesday DateTime today = DateTime.Today; // The (... + 7) % 7 ensures we end up with a value in the range [0, 6] int daysUntilMonday = ((int)DayOfWeek.Monday - (int)today.DayOfWeek + 7) % 7; DateTime nextMonday = today.AddDays(daysUntilMonday); List mondays = new List (); mondays.Add(nextMonday.ToLongDateString()); for (int i = 0; i < countOfWeeks; i++) { nextMonday = nextMonday.AddDays(7); mondays.Add(nextMonday.ToLongDateString()); } return mondays; }
...而且,如果你想将实际日期添加到combobox中,你可以像这样使用一个字典:
int WEEKS_TO_OFFER_COUNT = 13; BindingSource bs = new BindingSource(); Dictionary schedulableWeeks = AYttFMConstsAndUtils.GetWeekBeginningsDict(WEEKS_TO_OFFER_COUNT); bs.DataSource = schedulableWeeks; comboBoxWeekToSchedule.DataSource = bs; comboBoxWeekToSchedule.DisplayMember = "Key"; comboBoxWeekToSchedule.ValueMember = "Value"; public static Dictionary GetWeekBeginningsDict(int countOfWeeks) { DateTime today = DateTime.Today; // The (... + 7) % 7 ensures we end up with a value in the range [0, 6] int daysUntilMonday = ((int)DayOfWeek.Monday - (int)today.DayOfWeek + 7) % 7; DateTime nextMonday = today.AddDays(daysUntilMonday); Dictionary mondays = new Dictionary(); mondays.Add(nextMonday.ToLongDateString(), nextMonday); for (int i = 0; i < countOfWeeks; i++) { nextMonday = nextMonday.AddDays(7); mondays.Add(nextMonday.ToLongDateString(), nextMonday); } return mondays; }