【问题标题】:Binding a generic List<string> to a ComboBox将通用 List<string> 绑定到 ComboBox
【发布时间】:2010-12-09 07:15:52
【问题描述】:

我有一个 ComboBox,我想将一个通用列表绑定到它。谁能明白为什么下面的代码不起作用?绑定源中有数据,但不会填充 ComboBox 数据源。

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

附带说明:传递一个类的实例是不是很糟糕?

谢谢!

【问题讨论】:

  • 你看到了什么?您是否分配了 DisplayMember 属性?

标签: c# winforms data-binding binding


【解决方案1】:

你需要调用Bind方法:

cbxProjectd.DataBind();

如果这是针对 winforms 的,那么您需要确保调用的是您所拥有的,以下是有效的:

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

虽然你可以直接用列表设置ComboBox的DataSource。

【讨论】:

  • .DataBind 在哪里?它不会作为选项显示在智能感知中。
  • 它适用于 web 表单,对于 win 表单,您的答案应该有效。您的程序的另一部分有问题。如果你隔离你的代码,它应该可以工作。
  • 我只是做了更多的挖掘,发现了一个埋藏的异常,它确实冒了出来。谢谢大家。
  • @YuriyFaktorovich:请考虑删除答案的第一部分,因为他在问题中明确表示,这是针对 winforms 的。
【解决方案2】:

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

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;

【讨论】:

    【解决方案3】:
    BindingSource bs = new BindingSource();
    bs.DataSource = getprojectname();
    comboBox1 = new ComboBox();
    comboBox1.DataSource = bs;
    

    【讨论】:

      【解决方案4】:

      这是一个不使用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>());
      

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          【解决方案6】:

          如果有人发现这个 necro 线程,请确保您的列表不包含 null 项。 否则绑定会静默失败!

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

          【讨论】:

            猜你喜欢
            • 2011-11-11
            • 2012-12-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-12-07
            • 1970-01-01
            • 2013-10-04
            • 1970-01-01
            相关资源
            最近更新 更多