I've got a datagrid bound to a datatable, with a ComboBoxColumn. The XAML for this column is as follows:
<DataGridComboBoxColumn Header="Rep Name" SortMemberPath="RepName"
ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged, Source={StaticResource EmployeeList}, Path=Employees}"
SelectedValueBinding="{Binding Mode=TwoWay, Path=EmpId}"
SelectedValuePath="EmpId" DisplayMemberPath="RepName" />
My Employees class:
public class EmployeeList : INotifyPropertyChanged
{
private ObservableCollection<Employee> _employees = new ObservableCollection<Employee>();
public EmployeeList()
{
...
}
public ObservableCollection<Employee> Employees
{
get { return _employees; }
set { _employees = value; NotifyPropertyChanged("Employees"); }
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class Employee : INotifyPropertyChanged
{
private int _id;
public int EmpId
{
get { return _id; }
set { _id = value; OnPropertyChanged("EmpId"); }
}
public string RepName { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this,
new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
The DataTable serving as items source for the grid contains an "EmpId" column and a "RepName" column. The combobox is populated with all my employees, and when I make a selection, it is reflected in the datatable. However, when the screen loads, the currently assigned employee is not selected by default in the combobox. I thought that the SelectedValueBinding property of the combobox would handle this... what am I doing wrong?
Update for clarification:
The datagrid is bound to a datatable which includes an EmployeeID column. Let's assume that when the screen loads, there are three rows in the table with EmployeeIDs 1, 2, and 3. I need the combobox column in the first row to have EmployeeID 1 selected, the second row to have EmployeeID 2 selected, and the third row to have EmployeeID 3 selected.