How to: Customize Auto-Generated Columns in the DataGrid Control

Silverlight

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

  1. Create an event handler for the DataGrid's AutoGeneratingColumn event.

    
    Private Sub dataGrid1_AutoGeneratingColumn(ByVal sender As System.Object, ByVal e As System.Windows.Controls.DataGridAutoGeneratingColumnEventArgs)
    
    
    ...
    
    
    End Sub
    
    
    
  2. Add the event handler to the DataGrid instances events.

    <sdk:DataGrid x:Name="dataGrid1"
                AutoGenerateColumns="True"
                AutoGeneratingColumn="dataGrid1_AutoGeneratingColumn"/>
    

To modify a generated column

To replace a generated column

  1. In the AutoGeneratingColumn event handler, create a new DataGridColumn.

    
    '  Replace the DueDate column with a custom template column.
    If e.PropertyName = "DueDate" Then
        ' Create a new template column.
        Dim templateColumn As New DataGridTemplateColumn
        templateColumn.Header = "Due Date"
        templateColumn.CellTemplate = Me.Resources("dueDateCellTemplate")
        templateColumn.CellEditingTemplate = Me.Resources("dueDateCellEditingTemplate")
        templateColumn.SortMemberPath = "DueDate"
        ' ...
    
    
    
  2. Replace the column from the DataGridAutoGeneratingColumnEventArgs.Column property with the new DataGridColumn instance.

    
    ' Replace the auto-generated column with the templateColumn.
    e.Column = templateColumn
    
    
    

To cancel generation of a column

  • Set the Cancel property to true.

    
    ' Cancel AutoGeneration of all boolean columns.
    If e.PropertyType Is GetType(Boolean) Then
        e.Cancel = True
    End If
    
    
    

The following code example creates a List(Of 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>



Partial Public Class Page
    Inherits UserControl

    Public Sub New()
        InitializeComponent()
        '  Generate some task data and add it to the task list.
        Dim taskList As List(Of Task) = New List(Of Task)
        For index = 1 To 10
            taskList.Add(New Task() With _
                         {.Name = "Task " & index.ToString(), _
                           .DueDate = Date.Now.AddDays(index), _
                           .Complete = (index Mod 3 = 0), _
                           .Notes = "add notes..." _
                         })
        Next
        Me.dataGrid1.ItemsSource = taskList
    End Sub
    Private Sub dataGrid1_AutoGeneratingColumn(ByVal sender As System.Object, ByVal e As System.Windows.Controls.DataGridAutoGeneratingColumnEventArgs)
        ' Modify the header of the Name column.
        If e.Column.Header.ToString() = "Name" Then
            e.Column.Header = "Task"
        End If
        '  Replace the DueDate column with a custom template column.
        If e.PropertyName = "DueDate" Then
            ' Create a new template column.
            Dim templateColumn As New DataGridTemplateColumn
            templateColumn.Header = "Due Date"
            templateColumn.CellTemplate = Me.Resources("dueDateCellTemplate")
            templateColumn.CellEditingTemplate = Me.Resources("dueDateCellEditingTemplate")
            templateColumn.SortMemberPath = "DueDate"
            ' ...
            ' Replace the auto-generated column with the templateColumn.
            e.Column = templateColumn

        End If
        ' Cancel AutoGeneration of all boolean columns.
        If e.PropertyType Is GetType(Boolean) Then
            e.Cancel = True
        End If
    End Sub
End Class

Public Class Task
    Private m_Name As String
    Private m_DueDate As Date
    Private m_Complete As Boolean
    Private m_Notes As String

    Property Name() As String
        Get
            Return Me.m_Name
        End Get
        Set(ByVal value As String)
            Me.m_Name = value
        End Set
    End Property

    Property DueDate() As Date
        Get
            Return Me.m_DueDate
        End Get
        Set(ByVal value As Date)
            Me.m_DueDate = value
        End Set
    End Property

    Property Complete() As Boolean
        Get
            Return Me.m_Complete
        End Get
        Set(ByVal value As Boolean)
            Me.m_Complete = value
        End Set
    End Property

    Property Notes() As String
        Get
            Return Me.m_Notes
        End Get
        Set(ByVal value As String)
            Me.m_Notes = value
        End Set
    End Property
End Class


To view a running sample of the DataGrid control, click the following link.

Run this sample

Community Additions

ADD
Show: