Export (0) Print
Expand All
Expand Minimize

DataGrid.AutoGenerateColumns Property

Gets or sets a value that indicates whether the columns are created automatically.

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

public bool AutoGenerateColumns { get; set; }
<object AutoGenerateColumns="bool" .../>

Property Value

Type: System.Boolean
true if columns are created automatically; otherwise, false. The registered default is true. For more information about what can influence the value, see DependencyProperty.

Setting AutoGenerateColumns to true or false creates or removes auto-generated columns. When the AutoGenerateColumns property is set to true, the AutoGeneratingColumn event will be raised for each column that is created. You can change or cancel the column being created in the AutoGeneratingColumn event handler.

When auto-generation of all columns is complete, the AutoGeneratedColumns event is raised.

NoteNote

DataGrid cannot auto-generate columns when the source is XML data. In this case, create a custom Columns collection. For an example, see DataGridTextColumn.

The following example shows how to auto-generate columns and change the columns at creation time in the AutoGeneratingColumn event handler.

<!-- The DataGrid.DataContext is a DataTable that contains a list of customers.  The DataTable columns are  
     Title, FirstName, MiddleName, LastName, Suffix, CompanyName, EmailAddress, and Phone.-->
<DataGrid Name="DG1" ItemsSource="{Binding}" AutoGenerateColumns="True" AutoGeneratingColumn="DG1_AutoGeneratingColumn" />
//Access and update columns during autogeneration 
private void DG1_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    string headername = e.Column.Header.ToString();

    //Cancel the column you don't want to generate 
    if (headername == "MiddleName")
    {
        e.Cancel = true;
    }

    //update column details when generating 
    if (headername == "FirstName")
    {
        e.Column.Header = "First Name";
    }
    else if (headername == "LastName")
    {
        e.Column.Header = "Last Name";
    }
    else if (headername == "EmailAddress")
    {
        e.Column.Header = "Email";

    }

}

.NET Framework

Supported in: 4.5.3, 4

.NET Framework Client Profile

Supported in: 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft