DataGrid.AutoGenerateColumns 屬性

定義

取得或設定值,這個值表示是否會自動建立資料行。Gets or sets a value that indicates whether the columns are created automatically.

public:
 property bool AutoGenerateColumns { bool get(); void set(bool value); };
public bool AutoGenerateColumns { get; set; }
member this.AutoGenerateColumns : bool with get, set
Public Property AutoGenerateColumns As Boolean

屬性值

Boolean

如果會自動建立資料行,則為 true,否則為 falsetrue if columns are created automatically; otherwise, false. 已註冊的預設值是 trueThe registered default is true. 如需哪些因素會影響值的詳細資訊,請參閱 DependencyPropertyFor more information about what can influence the value, see DependencyProperty.

範例

下列範例示範如何在事件處理常式中自動產生資料行,並在建立時變更資料行 AutoGeneratingColumnThe 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";
    }
}
'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

備註

將設定 AutoGenerateColumnstrue 或建立 false 或移除自動產生的資料行。Setting AutoGenerateColumns to true or false creates or removes auto-generated columns. AutoGenerateColumns 屬性設定為時 true ,就會 AutoGeneratingColumn 針對建立的每個資料行引發事件。When the AutoGenerateColumns property is set to true, the AutoGeneratingColumn event will be raised for each column that is created. 您可以變更或取消在事件處理常式中建立的資料行 AutoGeneratingColumnYou can change or cancel the column being created in the AutoGeneratingColumn event handler.

當所有資料行的自動產生都已完成時, AutoGeneratedColumns 就會引發事件。When auto-generation of all columns is complete, the AutoGeneratedColumns event is raised.

注意

DataGrid 當來源為 XML 資料時,無法自動產生資料行。DataGrid cannot auto-generate columns when the source is XML data. 在此情況下,請建立自訂 Columns 集合。In this case, create a custom Columns collection. 如需範例,請參閱 DataGridTextColumnFor an example, see DataGridTextColumn.

適用於

另請參閱