다음을 통해 공유


DataGrid.AutoGeneratingColumn 이벤트

정의

개별 열이 자동 생성될 때 발생합니다.

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) 

이벤트 유형

예제

다음 예제에서는 이벤트 처리기에서 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

설명

이벤트 처리기에서 생성되는 열을 변경하거나 취소할 AutoGeneratingColumn 수 있습니다.

모든 열이 자동으로 생성되면 AutoGeneratedColumns 이벤트가 발생합니다.

적용 대상

추가 정보