簡體   English   中英

綁定通用列表<string>到一個組合框

[英]Binding a generic List<string> to a ComboBox

我有一個 ComboBox,我想將一個通用列表綁定到它。 誰能看出為什么下面的代碼不起作用? 綁定源中有數據,但不會填充 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<string> { "test1", "test2" };
comboBox1.DataSource = bs;

雖然您可以直接使用列表設置 ComboBox 的數據源。

這是簡單的方法(它工作正常):

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;

這是一種不使用 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>());
BindingSource bs = new BindingSource();
bs.DataSource = getprojectname();
comboBox1 = new ComboBox();
comboBox1.DataSource = bs;

使用上面 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 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;
}

如果有人發現此死靈線程,請確保您的列表不包含空項目。 否則綁定將無聲無息地失敗!

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM