private void DataGrid_Loaded(object sender, RoutedEventArgs e)
{
this.DataGrid.AutoGenerateColumns = false;
this.DataGrid.ItemsSource = dt;
DataTemplate cellTemplate = new DataTemplate();
FrameworkElementFactory frameworkElement = new FrameworkElementFactory(typeof(ComboBox));
Binding displayBinding = new Binding() { Path = new PropertyPath("EmployeeID") };
frameworkElement.SetValue(ComboBox.ItemsSourceProperty, customerCollection);
frameworkElement.SetValue(ComboBox.DisplayMemberPathProperty, "ID");
frameworkElement.SetValue(ComboBox.SelectedValueProperty, displayBinding);
cellTemplate.VisualTree = frameworkElement;
this.DataGrid.Columns.Add(new GridTemplateColumn() { MappingName = "EmployeeID", CellTemplate = cellTemplate });
DataTemplate cellTemplate1 = new DataTemplate();
FrameworkElementFactory frameworkElement1 = new FrameworkElementFactory(typeof(ComboBox));
Binding displayBinding1 = new Binding() { Path = new PropertyPath("EmployeeName") };
frameworkElement1.SetValue(ComboBox.ItemsSourceProperty, customerCollection);
frameworkElement1.SetValue(ComboBox.DisplayMemberPathProperty, "Name");
frameworkElement.SetValue(ComboBox.SelectedValueProperty, displayBinding1);
cellTemplate1.VisualTree = frameworkElement1;
this.DataGrid.Columns.Add(new GridTemplateColumn() { MappingName = "EmployeeName", CellTemplate = cellTemplate1 });
this.DataGrid.Columns.Add(new GridTextColumn() { MappingName = "Destination" });
}
private void AddColumns_Click(object sender, RoutedEventArgs e)
{
dt.Columns.Add("EmployeeAge", typeof(int));
foreach (System.Data.DataRow row in dt.Rows)
{
row[3] = r.Next(25, 30);
}
this.DataGrid.Columns.Add(new GridTextColumn() { MappingName = "EmployeeAge" });
this.DataGrid.ItemsSource = dt;
} |