C# 绑定通用列表<;字符串>;到组合框

C# 绑定通用列表<;字符串>;到组合框,c#,winforms,data-binding,binding,C#,Winforms,Data Binding,Binding,我有一个组合框,我想绑定一个通用列表。有人知道为什么下面的代码不起作用吗?绑定源中包含数据,但它不会填充组合框数据源 FillCbxProject(DownloadData Down) { BindingSource bindingSource = new BindingSource(); bindingSource.DataSource = Down.ProjectList; cbxProjectd.DataSource = bindingSource; } 另一方面:传递类的实

我有一个组合框,我想绑定一个通用列表。有人知道为什么下面的代码不起作用吗?绑定源中包含数据,但它不会填充组合框数据源

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<string> { "test1", "test2" };
comboBox1.DataSource = bs;
BindingSource bs=newbindingsource();
bs.DataSource=新列表{“test1”、“test2”};
comboBox1.DataSource=bs;
尽管您可以直接使用列表设置组合框的数据源。

这是一种简单的方法(它工作正常):

List my_List=new List();
我的清单。添加(“第1项”);
我的清单。添加(“第2项”);
我的清单。添加(“第3项”);
我的清单。添加(“第4项”);
我的清单。添加(“第5项”);
comboBox1.DataSource=我的列表;

以下是一种不使用BindingSource的简单方法:

首先,将字符串的通用列表添加到“consts/utils”类中:

公共静态列表月份=新列表
{
“简”,
“二月”,
“三月”,
“四月”,
“五月”,
“六月”,
“七月”,
“八月”,
“九月”,
“十月”,
“11月”,
“十二月”
};
下面是如何将这些字符串添加到组合框:

comboBoxMonth.Items.AddRange(UsageRptConstsAndUtils.Months.ToArray<object>());
comboBoxMonth.Items.AddRange(UsageRptConstsAndUtils.Months.ToArray());

以上面Yuriy Faktorovich的代码为基础,下面介绍如何获取给定周数的LongDateString格式的日期列表,并将其分配给组合框。这使用“周一”,但您可以简单地用任何其他道琼斯指数替换“周一”,以满足您的目的:

private void PopulateSchedulableWeeks()
{
    int WEEKS_COUNT = 13;
    List<String> schedulableWeeks = PlatypusUtils.GetWeekBeginnings(WEEKS_COUNT).ToList();
    BindingSource bs = new BindingSource();
    bs.DataSource = schedulableWeeks;
    comboBoxWeekToSchedule.DataSource = bs;
}

public static List<String> 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<String> mondays = new List<string>();
    mondays.Add(nextMonday.ToLongDateString());

    for (int i = 0; i < countOfWeeks; i++)
    {
        nextMonday = nextMonday.AddDays(7);
        mondays.Add(nextMonday.ToLongDateString());
    }
    return mondays;
}
private void PopulateSchedulableWeeks()
{
整数周数=13;
List schedulableWeeks=PlatypusUtils.getweekbegings(WEEKS\u COUNT.ToList();
BindingSource bs=新的BindingSource();
bs.DataSource=可调度周;
comboBoxWeekToSchedule.DataSource=bs;
}
公共静态列表GetWeekBeginnings(整数周数)
{
//从http://stackoverflow.com/questions/6346119/datetime-get-next-tuesday
DateTime today=DateTime.today;
//(…+7)%7确保我们得到的值在[0,6]范围内
int daysUntilMonday=((int)DayOfWeek.Monday-(int)today.DayOfWeek+7)%7;
DateTime nextMonday=今天.AddDays(daysUntilMonday);
列表星期一=新列表();
Add(nextMonday.ToLongDateString());
for(int i=0;i
…而且,如果您还想将实际日期添加到组合框中,您可以使用如下字典:

    int WEEKS_TO_OFFER_COUNT = 13;
    BindingSource bs = new BindingSource();
    Dictionary<String, DateTime> schedulableWeeks = AYttFMConstsAndUtils.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;
    // 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<String, DateTime> mondays = new Dictionary<String, DateTime>();
    mondays.Add(nextMonday.ToLongDateString(), nextMonday);

    for (int i = 0; i < countOfWeeks; i++)
    {
        nextMonday = nextMonday.AddDays(7);
        mondays.Add(nextMonday.ToLongDateString(), nextMonday);
    }
    return mondays;
}
int WEEKS\u TO\u OFFER\u COUNT=13;
BindingSource bs=新的BindingSource();
Dictionary schedulableWeeks=AYttFMConstsAndUtils.GetWeekBeginningsDict(周数到提供数);bs.DataSource=可调度周;
comboBoxWeekToSchedule.DataSource=bs;
comboBoxWeekToSchedule.DisplayMember=“Key”;
comboBoxWeekToSchedule.ValueMember=“Value”;
公共静态字典GetWeekBeginningsDict(整数周数)
{
DateTime today=DateTime.today;
//(…+7)%7确保我们得到的值在[0,6]范围内
int daysUntilMonday=((int)DayOfWeek.Monday-(int)today.DayOfWeek+7)%7;
DateTime nextMonday=今天.AddDays(daysUntilMonday);
字典星期一=新字典();
添加(nextMonday.ToLongDateString(),nextMonday);
for(int i=0;i
如果有人发现此necro线程,请确保您的列表中不包含空项。 否则绑定将自动失败

//这行不通!
comboBox1.DataSource=新列表{“test1”,null,“test2”};
//这是合法的!
comboBox1.DataSource=新列表{“test1”、“”、“test2”};

您看到了什么?您分配了DisplayMember属性吗?DataBind在哪里?在intellisense中,它不是一个选项。它是针对web表单的,对于win表单,您的答案应该有效。程序的另一部分有问题。如果你把你的代码隔离出来,它应该会工作的。我只是做了更多的挖掘,发现了一个隐藏的异常,它确实是从理性中冒出来的。谢谢大家。请考虑删除你的答案的第一部分,因为他在他的问题中明确指出,这是为WiFrices。
comboBoxMonth.Items.AddRange(UsageRptConstsAndUtils.Months.ToArray<object>());
private void PopulateSchedulableWeeks()
{
    int WEEKS_COUNT = 13;
    List<String> schedulableWeeks = PlatypusUtils.GetWeekBeginnings(WEEKS_COUNT).ToList();
    BindingSource bs = new BindingSource();
    bs.DataSource = schedulableWeeks;
    comboBoxWeekToSchedule.DataSource = bs;
}

public static List<String> 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<String> mondays = new List<string>();
    mondays.Add(nextMonday.ToLongDateString());

    for (int i = 0; i < countOfWeeks; i++)
    {
        nextMonday = nextMonday.AddDays(7);
        mondays.Add(nextMonday.ToLongDateString());
    }
    return mondays;
}
    int WEEKS_TO_OFFER_COUNT = 13;
    BindingSource bs = new BindingSource();
    Dictionary<String, DateTime> schedulableWeeks = AYttFMConstsAndUtils.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;
    // 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<String, DateTime> mondays = new Dictionary<String, DateTime>();
    mondays.Add(nextMonday.ToLongDateString(), nextMonday);

    for (int i = 0; i < countOfWeeks; i++)
    {
        nextMonday = nextMonday.AddDays(7);
        mondays.Add(nextMonday.ToLongDateString(), nextMonday);
    }
    return mondays;
}