DataGrid.Columns Property

Definition

Gets a collection that contains all the columns in the DataGrid.

C#
public System.Collections.ObjectModel.ObservableCollection<System.Windows.Controls.DataGridColumn> Columns { get; }

Property Value

The collection of columns in the DataGrid.

Examples

The following example shows how to add a column to the collection.

XAML
<!-- 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" />
C#
public Window1()
{
C#
    //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);
}

The following example shows how to remove a column from the collection.

XAML
<Button Content="Delete First Column" Click="Button_Click" />
XAML
<!-- 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"  />
C#
private void Button_Click(object sender, RoutedEventArgs e)
{
    //Delete the first column whether visible or not
    DG1.Columns.RemoveAt(0);
}

The following example shows how to set properties on columns in the collection when they are auto-generated and when an event occurs.

XAML
<CheckBox Content="View Customer Details" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" />
XAML
<!-- 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"  />
C#
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;
        }
    }
}

Remarks

Use the Columns collection to add columns, remove columns, or update properties on the columns.

Note

The order of columns in the collection does not determine the order that they will appear in the DataGrid. The DisplayIndex property determines the column order.

Each column in the Columns collection defines a column in the DataGrid. The following table lists the four column types that the DataGrid provides.

Column Type Data Display
DataGridHyperlinkColumn Use to display URI data.
DataGridComboBoxColumn Use to display enumeration data.
DataGridTextColumn Use to display text.
DataGridCheckBoxColumn Use to display Boolean data.

In addition, you can define your own custom column by using DataGridTemplateColumn. Columns in the collection must derive from DataGridColumn. Note that DataGridBoundColumn, which adds support for binding, derives from DataGridColumn and is the base for several of the defined column types.

All columns in the collection use the ItemsSource property defined by the DataGrid.

You can modify the Columns collection at run time regardless of whether it contains generated columns.

Applies to

Product Versions
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9