Compartilhar via


DataGrid.AutoGeneratingColumn Evento

Definição

Ocorre quando uma coluna individual é gerada automaticamente.

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) 

Tipo de evento

Exemplos

O exemplo a seguir mostra como gerar colunas automaticamente e alterar as colunas no momento da criação no AutoGeneratingColumn manipulador de eventos.

<!-- 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

Comentários

Você pode alterar ou cancelar a coluna que está sendo criada no AutoGeneratingColumn manipulador de eventos.

Quando todas as colunas são geradas automaticamente, o AutoGeneratedColumns evento ocorre.

Aplica-se a

Confira também