Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.
This topic has not yet been rated - Rate this topic

DataGrid.Columns Property

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

Namespace:  System.Windows.Controls
Assembly:  System.Windows.Controls.Data (in System.Windows.Controls.Data.dll)
public ObservableCollection<DataGridColumn> Columns { get; }
<sdk:DataGrid>
  <sdk:DataGrid.Columns>
    oneOrMoreColumns
  </sdk:DataGrid.Columns>
</sdk:DataGrid>

XAML Values

oneOrMoreColumns

One or more object elements for classes that derive from DataGridColumn. Typically these are one of the following Silverlight classes: DataGridTemplateColumn, DataGridCheckBoxColumn, DataGridTextColumn.

Property Value

Type: System.Collections.ObjectModel.ObservableCollection<DataGridColumn>
The columns in the control.

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

Each column in the Columns collection defines a column in the DataGrid. Columns in the collection must derive from DataGridColumn. DataGridBoundColumn, which adds support for binding, derives from DataGridColumn and is the base for the defined column types.

The following table lists the two defined column types that the DataGrid provides.

Column type

Data type

DataGridTextColumn

Used to display text.

DataGridCheckBoxColumn

Used to display Boolean data.

In addition, you can define your own custom column by using DataGridTemplateColumn .

You can modify the Columns collection at run time regardless of whether it contains generated columns. However, if you specify columns in XAML, you should not set AutoGenerateColumns to true.

You cannot add a column to the Columns collection of more than one DataGrid. If you try to do this, an InvalidOperationException will be thrown.

If you try to modify the Columns collection while the DataGrid is in editing mode, the control will automatically commit the edit. If the control cannot commit the edit, it will throw an InvalidOperationException.

The following code example demonstrates how to populate the Columns collection in XAML. This example is part of a larger example available in the DataGrid class overview.


<sdk:DataGrid x:Name="dataGrid4" 
    Height="160" Margin="0,5,0,10" 
    RowHeight="40" AutoGenerateColumns="False" >    
    <sdk:DataGrid.Columns>
        <sdk:DataGridTextColumn 
            Header="First Name" 
            Width="SizeToHeader"
            Binding="{Binding FirstName}" 
            FontSize="20" />
        <sdk:DataGridTextColumn 
            Header="Last Name" 
            Width="SizeToCells"
            Binding="{Binding LastName}" 
            FontSize="20" />
        <sdk:DataGridTextColumn 
            Header="Address"
            Width="150"
            Binding="{Binding Address}" >
            <sdk:DataGridTextColumn.ElementStyle>
                <Style TargetType="TextBlock">
                    <Setter Property="TextWrapping" Value="Wrap"/>
                </Style>
            </sdk:DataGridTextColumn.ElementStyle>
            <sdk:DataGridTextColumn.EditingElementStyle>
                <Style TargetType="TextBox">
                    <Setter Property="Foreground" Value="Blue"/>
                </Style>
            </sdk:DataGridTextColumn.EditingElementStyle>
        </sdk:DataGridTextColumn>
        <sdk:DataGridCheckBoxColumn 
            Header="New?" 
            Width="40"
            Binding="{Binding IsNew}" />
        <sdk:DataGridCheckBoxColumn 
            Header="Subscribed?" 
            Width="Auto"
            Binding="{Binding IsSubscribed}" 
            IsThreeState="True" />
    </sdk:DataGrid.Columns>
</sdk:DataGrid>


Silverlight

Supported in: 5, 4, 3

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft. All rights reserved.