【问题标题】:Bind data to combobox将数据绑定到组合框
【发布时间】:2013-01-28 14:41:53
【问题描述】:

在这个加载函数中有一些酒店名称,我想将这些酒店名称绑定到组合框。我走了几个步骤,但我在将值绑定到组合框时遇到了问题。

private void myWindow_Load(object sender, EventArgs e)
{
    string new1 = f.Name; //hotel names comes here(eg:hilton,serandib)

    List<string> Hotels = new1 List<string>();
    Hotels.Add(new1);
    foreach (string Hotel in Hotels)
    {

    }
 }

实际上我希望这个酒店名称显示在组合框中。(这是一个 Windows 窗体),请帮我解决其余的问题。

【问题讨论】:

标签: c# data-binding .net-4.0 combobox


【解决方案1】:

您将要添加一个项目到ComboBox,但实际上您不需要使用List&lt;string&gt; 来列出项目到ComboBox,您可以直接在.ItemsComboBox 中进行操作

 string new1 = f.Name; //hotel names comes here(eg:hilton,serandib)
 comboBox5.Items.Add(new1);

【讨论】:

    【解决方案2】:
            List<string> names = new List<string>();
            names.Add("name1");
            names.Add("name2");
            names.Add("name3");
            names.Add("name4");
            names.Add("name5");
            comboBox1.Items.Clear();
            foreach (string name in names)
            {
                comboBox1.Items.Add(name);
            }
            comboBox1.SelectedIndex = 0; //selects first item
    

    【讨论】:

      【解决方案3】:
      List<Hotels> Hname = new List<Hotels> { "Taj", " Star", "Poorna" ,"Settinad" };
      comboBox.DataSource =  Hname;
      

      List<Hotels> Hotel = new List<Hotels>();
      Hotel.Add( "name");
      
      comboBox.DataSource = Hotel;
      

      【讨论】:

        【解决方案4】:

        您可以使用以下代码,

        ComboBox1.DataSource = hotelList;
        

        如果您有来自f.Name的以下字符串

        “经络,财富,韩亚”

            List<String> hotelList = f.Name.Split(',').ToList();
        
            ComboBox1.DataSource = hotelList;
        

        【讨论】:

        • 只要把所有的名字都收集到一个列表里然后绑定..如前所述!
        猜你喜欢
        • 2013-12-24
        • 2013-09-03
        • 2013-05-21
        • 1970-01-01
        • 2013-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-02
        相关资源
        最近更新 更多