Partager via


DataGrid.AutoGeneratingColumn Événement

Définition

Se produit lorsqu'une colonne individuelle est générée automatiquement.

public:
 event EventHandler<System::Windows::Controls::DataGridAutoGeneratingColumnEventArgs ^> ^ AutoGeneratingColumn;
public event EventHandler<System.Windows.Controls.DataGridAutoGeneratingColumnEventArgs> AutoGeneratingColumn;
member this.AutoGeneratingColumn : EventHandler<System.Windows.Controls.DataGridAutoGeneratingColumnEventArgs> 
Public Custom Event AutoGeneratingColumn As EventHandler(Of DataGridAutoGeneratingColumnEventArgs) 
Public Event AutoGeneratingColumn As EventHandler(Of DataGridAutoGeneratingColumnEventArgs) 

Type d'événement

Exemples

L’exemple suivant montre comment générer automatiquement des colonnes et modifier les colonnes au moment de la création dans le gestionnaire d’événements AutoGeneratingColumn .

<!-- 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";
    }
}
'Access and update columns during autogeneration
Private Sub DG1_AutoGeneratingColumn(ByVal sender As Object, ByVal e As DataGridAutoGeneratingColumnEventArgs)
    Dim headername As String = e.Column.Header.ToString()
    'Cancel the column you don't want to generate
    If headername = "MiddleName" Then
        e.Cancel = True
    End If

    'update column details when generating
    If headername = "FirstName" Then
        e.Column.Header = "First Name"
    ElseIf headername = "LastName" Then
        e.Column.Header = "Last Name"
    ElseIf headername = "EmailAddress" Then
        e.Column.Header = "Email"
    End If
End Sub

Remarques

Vous pouvez modifier ou annuler la colonne en cours de création dans le AutoGeneratingColumn gestionnaire d’événements.

Lorsque toutes les colonnes sont générées automatiquement, l’événement AutoGeneratedColumns se produit.

S’applique à

Voir aussi