将泛型List<string>绑定到ComboBox

18

我有一个ComboBox,想要将一个通用列表绑定到它上面。有人能看出为什么下面的代码不起作用吗?绑定源中有数据,但它不能填充ComboBox数据源。

FillCbxProject(DownloadData Down)
{
  BindingSource bindingSource = new BindingSource();
  bindingSource.DataSource = Down.ProjectList;
  cbxProjectd.DataSource = bindingSource;
}

顺便问一下:传递一个类的实例是不好的吗?

谢谢!


你看到什么了吗?你有分配 DisplayMember 属性吗? - Jacob Seleznev
6个回答

34

您需要调用Bind方法:

cbxProjectd.DataBind();

如果这是针对winforms的话,你需要确保所调用的内容是正确的,下面代码可以正常工作:

BindingSource bs = new BindingSource();
bs.DataSource = new List<string> { "test1", "test2" };
comboBox1.DataSource = bs;

虽然您可以直接使用列表设置ComboBox的数据源。


3
".DataBind"在哪里?它在智能感知中没有显示为选项。 - Nathan
这是针对Web表单的,对于Win表单,您的答案应该适用。您的程序的另一部分出了问题。如果您隔离代码,它应该可以工作。 - Yuriy Faktorovich
我刚刚做了更深入的挖掘,找到了一个被隐藏起来的异常,它是由于某个原因而冒泡上来的。谢谢大家。 - Nathan
1
@YuriyFaktorovich:请考虑删除您回答的第一部分,因为他在问题中明确说明了这是针对winforms的。 - Jannik

5

这是简单的方式(它可以正常工作):

List<string> my_list = new List<string>();
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;

2

这里有一种不使用BindingSource的相对简单的方法:

首先,添加字符串的泛型列表,可能添加到“consts/utils”类中:

public static List<string> Months = new List<string>
{
   "Jan",
   "Feb",
   "Mar",
   "Apr",
   "May",
   "Jun",
   "Jul",
   "Aug",
   "Sep",
   "Oct",
   "Nov",
   "Dec"
};

以下是如何将这些字符串添加到组合框中:

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

0

以Yuriy Faktorovich的代码为基础,以下是如何获取给定周数的LongDateString格式日期列表,并将它们分配给组合框。这里使用“星期一”,但您可以根据需要将“星期一”替换为任何其他DOW:

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 https://dev59.com/QWw15IYBdhLWcg3w3vpe
    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;
}

0
BindingSource bs = new BindingSource();
bs.DataSource = getprojectname();
comboBox1 = new ComboBox();
comboBox1.DataSource = bs;

0

如果有人找到这个已死的线程,请确保您的列表不包含空项。否则绑定将会静默地失败!

//This will not work!
comboBox1.DataSource = new List<string> { "test1", null, "test2" };

//This is legit!
comboBox1.DataSource = new List<string> { "test1", "", "test2" };

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接