Zackk
Zackk

Reputation: 73

How to bind data to ComboBox from entity

I have this code, but there is still no data in ComboBox.

using (var context = new DatabaseContext())
{
      var xd = (from c in context.CHall
                orderby c.CinemaHallName
                select c).ToList();
      foreach (var c in xd)
      {
           cbCinemaHall.ItemsSource = c.CinemaHallName;
           cbCinemaHall.DisplayMemberPath = "CinemaHallName";
      }                   
}

Upvotes: 0

Views: 188

Answers (1)

Salah Akbari
Salah Akbari

Reputation: 39946

You don't need foreach for this purpose. Just set the ItemsSource of your ComboBox to xd. Like this:

using (var context = new DatabaseContext())
{
     var xd = (from c in context.CHall
               orderby c.CinemaHallName
               select c).ToList();
     cbCinemaHall.ItemsSource = xd;
     cbCinemaHall.DisplayMemberPath = "CinemaHallName"; 
}

Upvotes: 1

Related Questions