Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
AutoGenerateColumns Property

DataGrid.AutoGenerateColumns Property

Gets or sets a value that indicates whether columns are created automatically when the ItemsSource property is set.

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

public bool AutoGenerateColumns { get; set; }
<sdk:DataGrid AutoGenerateColumns="bool"/>

Property Value

Type: System.Boolean
true if columns are generated automatically; otherwise, false. The default is true.

Dependency property identifier field: AutoGenerateColumnsProperty

You can modify the Columns collection at run time regardless of whether it contains generated columns. However, if you specify columns in XAML, you should set AutoGenerateColumns to false.

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

public MainPage()
    DG.DataContext = this.LayoutRoot.Children;

private void DG_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    string headername = e.Column.Header.ToString();

    switch (headername)
        // Cancel the columns you don't want to generate.
        case "Effect":
        case "Clip":
        case "Projection":
        case "OpacityMask":
        case "RenderTransformOrigin":
            e.Cancel = true;

        // Update column headers when generating.
        case "Opacity":

            e.Column.Header = "Opacity Value";

        case "UseLayoutRounding":

            e.Column.Header = "Layout Rounding?";

        case "IsHitTestVisible":

            e.Column.Header = "Hit Test Visible?";

        case "RenderSize":

            e.Column.Header = "Rendered Size";


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



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

© 2015 Microsoft