How to: Customize Auto-Generated Columns in the DataGrid Control
You can handle the AutoGeneratingColumn event to modify, replace, or cancel a column that is being generated. The AutoGeneratingColumn event occurs one time for each public, non-static property in the bound data type when the ItemsSource property is changed and the AutoGenerateColumns property is true.
To handle the AutoGeneratingColumn event
-
Create an event handler for the DataGrid's AutoGeneratingColumn event.
-
Add the event handler to the DataGrid instances events.
<sdk:DataGrid x:Name="dataGrid1" AutoGenerateColumns="True" AutoGeneratingColumn="dataGrid1_AutoGeneratingColumn"/>
To modify a generated column
-
In the AutoGeneratingColumn event handler, access the DataGridColumn properties by referencing the DataGridAutoGeneratingColumnEventArgs.Column property.
To replace a generated column
-
In the AutoGeneratingColumn event handler, create a new DataGridColumn.
// Replace the DueDate column with a custom template column. if (e.PropertyName == "DueDate") { // Create a new template column. DataGridTemplateColumn templateColumn = new DataGridTemplateColumn(); templateColumn.Header = "Due Date"; templateColumn.CellTemplate = (DataTemplate)Resources["dueDateCellTemplate"]; templateColumn.CellEditingTemplate = (DataTemplate)Resources["dueDateCellEditingTemplate"]; templateColumn.SortMemberPath = "DueDate"; // ...
-
Replace the column from the DataGridAutoGeneratingColumnEventArgs.Column property with the new DataGridColumn instance.
To cancel generation of a column
-
Set the Cancel property to true.
The following code example creates a List<T> of Task objects, and displays the task list in a DataGrid.named dataGrid1. In the AutoGeneratingColumn event handler of dataGrid1, the following customizations are made.
-
The Header of the 'Name' column is modified to display 'Task'.
-
The auto-generated DueDate column is replaced with a custom DataGridTemplateColumn.
-
The auto generation of all columns that display Boolean data is canceled.
<!-- NOTE: By convention, the sdk prefix indicates a URI-based XAML namespace declaration for Silverlight SDK client libraries. This namespace declaration is valid for Silverlight 4 only. In Silverlight 3, you must use individual XAML namespace declarations for each CLR assembly and namespace combination outside the scope of the default Silverlight XAML namespace. For more information, see the help topic "Prefixes and Mappings for Silverlight Libraries". --> <UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="DGAutoGenColumn.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="400" Height="300"> <UserControl.Resources> <DataTemplate x:Key="dueDateCellTemplate"> <TextBlock Text="{Binding DueDate}" Margin="5,4,5,4" /> </DataTemplate> <DataTemplate x:Key="dueDateCellEditingTemplate"> <sdk:DatePicker SelectedDate="{Binding DueDate, Mode=TwoWay}" /> </DataTemplate> </UserControl.Resources> <StackPanel x:Name="LayoutRoot" Background="White" Margin="5"> <TextBlock Text="Column AutoGeneration" Foreground="#FF5C9AC9" FontSize="18" FontFamily="Verdana" FontWeight="Bold" /> <sdk:DataGrid x:Name="dataGrid1" AutoGenerateColumns="True" AutoGeneratingColumn="dataGrid1_AutoGeneratingColumn" /> </StackPanel> </UserControl>
using System; using System.Collections.Generic; using System.Windows; using System.Windows.Controls; namespace DGAutoGenColumn { public partial class Page : UserControl { public Page() { InitializeComponent(); // Generate some task data and add it to the task list. List<Task> taskList = new List<Task>(); for (int i = 1; i <= 10; i++) { taskList.Add(new Task() { Name = "Task " + i.ToString(), DueDate = DateTime.Now.AddDays(i), Complete = (i % 3 == 0), Notes = "add notes..." }); } this.dataGrid1.ItemsSource = taskList; } private void dataGrid1_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) { // Modify the header of the Name column. if (e.Column.Header.ToString() == "Name") e.Column.Header = "Task"; // Replace the DueDate column with a custom template column. if (e.PropertyName == "DueDate") { // Create a new template column. DataGridTemplateColumn templateColumn = new DataGridTemplateColumn(); templateColumn.Header = "Due Date"; templateColumn.CellTemplate = (DataTemplate)Resources["dueDateCellTemplate"]; templateColumn.CellEditingTemplate = (DataTemplate)Resources["dueDateCellEditingTemplate"]; templateColumn.SortMemberPath = "DueDate"; // ... // Replace the auto-generated column with the templateColumn. e.Column = templateColumn; } // Cancel AutoGeneration of all boolean columns. if (e.PropertyType == typeof(bool)) e.Cancel = true; } } public class Task { public string Name { get; set; } public DateTime DueDate { get; set; } public bool Complete { get; set; } public string Notes { get; set; } } }
To view a running sample of the DataGrid control, click the following link.