Export (0) Print
Expand All

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)

public event EventHandler<DataGridAutoGeneratingColumnEventArgs> AutoGeneratingColumn
<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 MainPage()
{
    InitializeComponent();
    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;
            break;

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

            e.Column.Header = "Opacity Value";
            break;

        case "UseLayoutRounding":

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

        case "IsHitTestVisible":

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

        case "RenderSize":

            e.Column.Header = "Rendered Size";
            break;
    }

}



    <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:
© 2014 Microsoft