DataGrid.Columns 속성

정의

DataGrid의 모든 열을 포함하는 컬렉션을 가져옵니다.Gets a collection that contains all the columns in the DataGrid.

public:
 property System::Collections::ObjectModel::ObservableCollection<System::Windows::Controls::DataGridColumn ^> ^ Columns { System::Collections::ObjectModel::ObservableCollection<System::Windows::Controls::DataGridColumn ^> ^ get(); };
public System.Collections.ObjectModel.ObservableCollection<System.Windows.Controls.DataGridColumn> Columns { get; }
member this.Columns : System.Collections.ObjectModel.ObservableCollection<System.Windows.Controls.DataGridColumn>
Public ReadOnly Property Columns As ObservableCollection(Of DataGridColumn)

속성 값

ObservableCollection<DataGridColumn>

DataGrid에 있는 열의 컬렉션입니다.The collection of columns in the DataGrid.

예제

다음 예제에서는 컬렉션에 열을 추가 하는 방법을 보여 줍니다.The following example shows how to add a column to the collection.

<!-- ItemsSource is a DataTable that contains a list of customers.  The DataTable columns are  
     Title, FirstName, MiddleName, LastName, Suffix, CompanyName, EmailAddress, and Phone.-->
<DataGrid Grid.Row="2" Name="DG2" ItemsSource="{Binding}" AutoGenerateColumns="False" />
public Window1()
{
Public Sub New()
    //Create a new column to add to the DataGrid
    DataGridTextColumn textcol = new DataGridTextColumn();
    //Create a Binding object to define the path to the DataGrid.ItemsSource property
    //The column inherits its DataContext from the DataGrid, so you don't set the source
    Binding b = new Binding("LastName");
    //Set the properties on the new column
    textcol.Binding = b;
    textcol.Header = "Last Name";
    //Add the column to the DataGrid
    DG2.Columns.Add(textcol);
}
    'Create a new column to add to the DataGrid
    Dim textcol As New DataGridTextColumn()
    'Create a Binding object to define the path to the DataGrid.ItemsSource property 
    'The column inherits its DataContext from the DataGrid, so you don't set the source
    Dim b As New Binding("LastName")
    'Set the properties on the new column
    textcol.Binding = b
    textcol.Header = "Last Name"
    'Add the column to the DataGrid

    DG2.Columns.Add(textcol)
End Sub

다음 예제에서는 컬렉션에서 열을 제거 하는 방법을 보여 줍니다.The following example shows how to remove a column from the collection.

<Button Content="Delete First Column" Click="Button_Click" />
<!-- ItemsSource is a DataTable that contains a list of customers.  The DataTable columns are  
     Title, FirstName, MiddleName, LastName, Suffix, CompanyName, EmailAddress, and Phone.-->
<DataGrid Grid.Row="1" Name="DG1" ItemsSource="{Binding}" AutoGeneratingColumn="DG1_AutoGeneratingColumn"  />
private void Button_Click(object sender, RoutedEventArgs e)
{
    //Delete the first column whether visible or not
    DG1.Columns.RemoveAt(0);
}
Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    'Delete the first column whether visible or not
    DG1.Columns.RemoveAt(0)
End Sub

다음 예제에서는 열이 자동 생성 될 때 및 이벤트가 발생할 때 컬렉션의 열에 대 한 속성을 설정 하는 방법을 보여 줍니다.The following example shows how to set properties on columns in the collection when they are auto-generated and when an event occurs.

<CheckBox Content="View Customer Details" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" />
<!-- ItemsSource is a DataTable that contains a list of customers.  The DataTable columns are  
     Title, FirstName, MiddleName, LastName, Suffix, CompanyName, EmailAddress, and Phone.-->
<DataGrid Grid.Row="1" Name="DG1" ItemsSource="{Binding}" AutoGeneratingColumn="DG1_AutoGeneratingColumn"  />
private void DG1_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    //Set properties on the columns during auto-generation
    switch (e.Column.Header.ToString())
    {
        case "LastName":
            e.Column.CanUserSort = false;
            e.Column.Visibility = Visibility.Visible;
            break;
        case "FirstName":
            e.Column.Visibility = Visibility.Visible;
            break;
        case "CompanyName":
            e.Column.Visibility = Visibility.Visible;
            break;
        case "EmailAddress":
            e.Column.Visibility = Visibility.Visible;
            break;
        default:
            e.Column.Visibility = Visibility.Collapsed;
            break;
    }
}

private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
    //Make each column in the collection visible
    foreach (DataGridColumn col in DG1.Columns)
    {
        col.Visibility = Visibility.Visible;
    }
}

private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
    //Get the columns collection
    ObservableCollection<DataGridColumn> columns = DG1.Columns;

    //set the visibility for each column so only 4 columns are visible
    foreach (DataGridColumn col in columns)
    {
        switch (col.Header.ToString())
        {
            case "LastName":
                col.Visibility = Visibility.Visible;
                break;
            case "FirstName":
                col.Visibility = Visibility.Visible;
                break;
            case "CompanyName":
                col.Visibility = Visibility.Visible;
                break;
            case "EmailAddress":
                col.Visibility = Visibility.Visible;
                break;
            default:
                col.Visibility = Visibility.Collapsed;
                break;
        }
    }
}
Private Sub DG1_AutoGeneratingColumn(ByVal sender As Object, ByVal e As DataGridAutoGeneratingColumnEventArgs)
    'Set properties on the columns during auto-generation
    Select Case e.Column.Header.ToString()
        Case "LastName"
            e.Column.CanUserSort = False
            e.Column.Visibility = Visibility.Visible
            Exit Select
        Case "FirstName"
            e.Column.Visibility = Visibility.Visible
            Exit Select
        Case "CompanyName"
            e.Column.Visibility = Visibility.Visible
            Exit Select
        Case "EmailAddress"
            e.Column.Visibility = Visibility.Visible
            Exit Select
        Case Else
            e.Column.Visibility = Visibility.Collapsed
            Exit Select

    End Select
End Sub

Private Sub CheckBox_Checked(ByVal sender As Object, ByVal e As RoutedEventArgs)
    'Make each column in the collection visible
    For Each col As DataGridColumn In DG1.Columns
        col.Visibility = Visibility.Visible
    Next
End Sub

Private Sub CheckBox_Unchecked(ByVal sender As Object, ByVal e As RoutedEventArgs)
    'Get the columns collection
    Dim columns As ObservableCollection(Of DataGridColumn) = DG1.Columns

    'set the visibility for each column so only 4 columns are visible
    For Each col As DataGridColumn In columns
        Select Case col.Header.ToString()
            Case "LastName"
                col.Visibility = Visibility.Visible
                Exit Select
            Case "FirstName"
                col.Visibility = Visibility.Visible
                Exit Select
            Case "CompanyName"
                col.Visibility = Visibility.Visible
                Exit Select
            Case "EmailAddress"
                col.Visibility = Visibility.Visible
                Exit Select
            Case Else
                col.Visibility = Visibility.Collapsed
                Exit Select
        End Select

    Next
End Sub

설명

컬렉션을 사용 하 여 열을 Columns 추가 하거나, 열을 제거 하거나, 열에 대 한 속성을 업데이트할 수 있습니다.Use the Columns collection to add columns, remove columns, or update properties on the columns.

참고

컬렉션의 열 순서는에 표시 되는 순서를 결정 하지 않습니다 DataGrid .The order of columns in the collection does not determine the order that they will appear in the DataGrid. DisplayIndex속성은 열 순서를 결정 합니다.The DisplayIndex property determines the column order.

컬렉션의 각 열 Columns 은의 열을 정의 DataGrid 합니다.Each column in the Columns collection defines a column in the DataGrid. 다음 표에서는에서 제공 하는 네 개의 열 형식을 보여 줍니다 DataGrid .The following table lists the four column types that the DataGrid provides.

열 유형Column Type 데이터 표시Data Display
DataGridHyperlinkColumn URI 데이터를 표시 하려면이 옵션을 사용 합니다.Use to display URI data.
DataGridComboBoxColumn 열거형 데이터를 표시 하려면이 옵션을 사용 합니다.Use to display enumeration data.
DataGridTextColumn 텍스트를 표시 하려면이 옵션을 사용 합니다.Use to display text.
DataGridCheckBoxColumn 부울 데이터를 표시 하려면이 옵션을 사용 합니다.Use to display Boolean data.

또한를 사용 하 여 고유한 사용자 지정 열을 정의할 수 있습니다 DataGridTemplateColumn .In addition, you can define your own custom column by using DataGridTemplateColumn. 컬렉션의 열은에서 파생 되어야 합니다 DataGridColumn .Columns in the collection must derive from DataGridColumn. DataGridBoundColumn바인딩에 대 한 지원을 추가 하는는에서 파생 되 DataGridColumn 고 정의 된 여러 열 형식에 대 한 기본입니다.Note that DataGridBoundColumn, which adds support for binding, derives from DataGridColumn and is the base for several of the defined column types.

컬렉션의 모든 열 ItemsSource 은에서 정의한 속성을 사용 DataGrid 합니다.All columns in the collection use the ItemsSource property defined by the DataGrid.

Columns생성 된 열이 포함 되어 있는지 여부에 관계 없이 런타임에 컬렉션을 수정할 수 있습니다.You can modify the Columns collection at run time regardless of whether it contains generated columns.

적용 대상