Export (0) Print
Expand All
Expand Minimize

DataGrid.Columns Property

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

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)

'Declaration
Public ReadOnly Property Columns As ObservableCollection(Of DataGridColumn)

Property Value

Type: System.Collections.ObjectModel.ObservableCollection(Of DataGridColumn)
The collection of columns in the DataGrid.

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

NoteNote

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.

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 Sub New()


...


    '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 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 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

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4
Show:
© 2015 Microsoft