New Posts New Posts RSS Feed: Populate ComboBox
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Populate ComboBox

 Post Reply Post Reply
Author
new user View Drop Down
Newbie
Newbie


Joined: 18-May-2011
Posts: 7
Post Options Post Options   Quote new user Quote  Post ReplyReply Direct Link To This Post Topic: Populate ComboBox
    Posted: 24-May-2011 at 9:52am
  Hello,
        
   My requirement is like this. First I need to get department id of all employess whose name is 'Alex'. Then i want to populate Combobox with department name of this department id. But this is on another entity. I have tried the following code to populate ComboBox with department name. It is populating ComboBox. But it returns same department name every time. (my combo box is in inside gridView)

     {
            .  .  .  .  .   .
            ItemSource.Clear();
            var query = (from emp in mgr.Employees.Where(i => i.EmpName == 'Alex')
                              select emp.DepartmentID);
           var op = query .ExecuteAsync();
           op.Completed += new EventHandler<EntityQueriedEventArgs<int?>>(op_Completed);
    
        }

        void op_Completed(object sender, EntityQueriedEventArgs<int?> e)
        {
             foreach (var r in e.Results)
            {
                var query2 = (from dep in mgr.Departments.Where(i => i.deptID == r)
                              select dep.DepartmentName);
                query2.AsScalarAsync().FirstOrNullEntity(op =>
                 {
                     ItemSource.Add(op.Result);
                 });
            }
            comboBox.ItemsSource = ItemSource; //but the ComboBox is populated with same data
        }

Please help me.
Back to Top
robertg View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 15-Mar-2011
Location: California
Posts: 87
Post Options Post Options   Quote robertg Quote  Post ReplyReply Direct Link To This Post Posted: 24-May-2011 at 2:27pm

Generally, what you want to do with this is to build your ItemsSource, and once that's done, specify the SelectedItem. For example, you could have a class like this:

 

namespace WpfComboBox {

class MainWindowViewModel {

public NorthwindIBEntities mgr;

public ObservableCollection<Employee> Employees { get; private set; }

public Employee CurrentEmployee;

public Employee CurrentManager;

public MainWindowViewModel() {

Employees = new ObservableCollection<Employee>();

mgr = new NorthwindIBEntities();

var query = mgr.Employees.ExecuteAsync(op => op.Results.ForEach(Employees.Add));

CurrentEmployee = mgr.Employees.FirstOrDefault();

CurrentManager = GetManager(CurrentEmployee);

}

private Employee GetManager(Employee theEmployee) {

int ManagerID = (int)theEmployee.ReportsToEmployeeID;

var query = mgr.Employees.Where(c => c.EmployeeID.Equals(ManagerID));

return query.FirstOrDefault();

}

}

}

And then, in your code for the display:

public MainWindow() {

InitializeComponent();

view = new MainWindowViewModel();

comboBox1.ItemsSource = view.Employees;

comboBox1.DisplayMemberPath = Employee.PathFor(e => e.MyFirstName);

comboBox1.SelectedItem = view.CurrentManager;

}

There's a pretty good sample showing ComboBox population in Silverlight, in the DRC. You can find that at http://drc.ideablade.com/xwiki/bin/view/Documentation/SimpleComboBox

Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down