Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Silverlight 3
DataGrid Class
DataGrid Properties
 Columns Property

  Switch on low bandwidth view
This page is specific to
Microsoft Silverlight 2

Other versions are also available for the following:
.NET Framework Class Library for Silverlight
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)
Visual Basic (Declaration)
Public ReadOnly Property Columns As ObservableCollection(Of DataGridColumn)
    Get
Visual Basic (Usage)
Dim instance As DataGrid
Dim value As ObservableCollection(Of DataGridColumn)

value = instance.Columns
C#
public ObservableCollection<DataGridColumn> Columns { get; }
XAML Property Element Usage
<data:DataGrid>
  <data:DataGrid.Columns>
    oneOrMoreColumns
  </data:DataGrid.Columns>
</data:DataGrid>

XAML Values

data:

A prefix that is defined to map the XML namespace for the System.Windows.Controls.Data assembly and the System.Windows.Controls CLR namespace.

oneOrMoreColumns

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

Property Value

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

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.

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

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

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker