Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

DataGrid.AutoGeneratingColumn Event

Occurs when an individual column is auto-generated.

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 event EventHandler<DataGridAutoGeneratingColumnEventArgs> AutoGeneratingColumn
<object AutoGeneratingColumn="EventHandler<DataGridAutoGeneratingColumnEventArgs>" .../>

You can change or cancel the column being created in the AutoGeneratingColumn event handler.

When all columns are auto-generated, the AutoGeneratedColumns event occurs.

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.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4
Show:
© 2015 Microsoft