2

In this Load function have some hotel names,I wanted to bind that hotel names to Combo box. I go several steps but i'm having a problem in bind values to combo box from here.

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)
    {

    }
 }

Actually i want this hotel names show in combo box.(This is a windows form),Help me with the rest.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Jehan Perera
  • 91
  • 3
  • 7

4 Answers4

2

You can make use of following code,

ComboBox1.DataSource = hotelList;

if you have following string coming from f.Name

"Le meridian, Fortune, Asiana"

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

    ComboBox1.DataSource = hotelList;
Rajesh Subramanian
  • 6,400
  • 5
  • 29
  • 42
1
List<Hotels> Hname = new List<Hotels> { "Taj", " Star", "Poorna" ,"Settinad" };
comboBox.DataSource =  Hname;

or

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

comboBox.DataSource = Hotel;
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
Pikachu
  • 27
  • 1
  • 6
1
        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
saeed
  • 2,477
  • 2
  • 23
  • 40
1

You're about to add a items to ComboBox but actually you don't need use the List<string> to list the items to ComboBox, you can go it directly in .Items of ComboBox

 string new1 = f.Name; //hotel names comes here(eg:hilton,serandib)
 comboBox5.Items.Add(new1);
spajce
  • 7,044
  • 5
  • 29
  • 44