DataGrid.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.

Namespace:  System.Windows.Controls
Assembly:  System.Windows.Controls.Data (in System.Windows.Controls.Data.dll)

'Declaration
Public Event AutoGeneratingColumn As EventHandler(Of DataGridAutoGeneratingColumnEventArgs)
<sdk:DataGrid AutoGeneratingColumn="eventhandler"/>

This event gives you the option of altering each generated column before it is added to the control. Additionally, you have the option of canceling the event to prevent specific columns from being added.

The following code example demonstrates how to set the AutoGenerateColumns property and handle the AutoGeneratingColumn event.


Public Sub New()
    InitializeComponent()
    DG.DataContext = Me.LayoutRoot.Children
End Sub

Private Sub DG_AutoGeneratingColumn(ByVal sender As Object, ByVal e As DataGridAutoGeneratingColumnEventArgs)
    Dim headername As String = e.Column.Header.ToString()

    Select Case headername
        ' Cancel the columns you don't want to generate.
        Case "Effect", "Clip", "Projection", "OpacityMask", "RenderTransformOrigin"
            e.Cancel = True
            Exit Select

            ' Update column headers when generating.
        Case "Opacity"

            e.Column.Header = "Opacity Value"
            Exit Select

        Case "UseLayoutRounding"

            e.Column.Header = "Layout Rounding?"
            Exit Select

        Case "IsHitTestVisible"

            e.Column.Header = "Hit Test Visible?"
            Exit Select

        Case "RenderSize"

            e.Column.Header = "Rendered Size"
            Exit Select

    End Select
End Sub



    <Grid x:Name="LayoutRoot" Background="White">
        <sdk:DataGrid AutoGenerateColumns="True" ItemsSource="{Binding}" 
		x:Name="DG" AutoGeneratingColumn="DG_AutoGeneratingColumn" />

    </Grid>


Silverlight

Supported in: 5, 4, 3

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show: