Export (0) Print
Expand All
Expand Minimize

DataGrid.FrozenColumnCount Property

Gets or sets the number of non-scrolling columns.

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

'Declaration
Public Property FrozenColumnCount As Integer
<object FrozenColumnCount="int" .../>

Property Value

Type: System.Int32
The number of non-scrolling columns. The registered default is 0. For more information about what can influence the value, see DependencyProperty.

Frozen columns are columns that are always displayed and cannot be scrolled out of visibility. Frozen columns are always the leftmost columns in display order. You cannot drag frozen columns into the group of unfrozen columns or drag unfrozen columns into the group of frozen columns.

To freeze columns, set the FrozenColumnCount property. The leftmost columns specified by the FrozenColumnCount number will become frozen. For example, if you set the FrozenColumnCount to 2, the two left columns in the display are frozen. To determine whether a column is already frozen, check the IsFrozen property on the DataGridColumn.

The following example shows how to make a column frozen when "Freeze Column" is selected from the ContextMenu. Columns are moved to the left in order to be included in the frozen columns.

<!--Defines the handlers for when the FreezeColumnCommand is executed-->
<Window.CommandBindings>
    <CommandBinding Command="{x:Static local:Window1.FreezeColumnCommand}" Executed="CommandBinding_Executed" />
</Window.CommandBindings>
<!--Defines the context menu for the ColumnHeaders and attaches the FreezeColumnCommand-->
<Window.Resources>
    <ContextMenu x:Key="ColumnHeaderMenu" IsEnabled="True"  >
        <MenuItem Header="Freeze Column" IsEnabled="True" Command="{x:Static local:Window1.FreezeColumnCommand}"
                  CommandTarget="{Binding RelativeSource={RelativeSource AncestorType=Popup}, Path=PlacementTarget}" 
                  CommandParameter="{Binding ElementName=DG1, Path=CurrentColumn.DisplayIndex}" />
    </ContextMenu>
</Window.Resources>

<Grid>
    <!--Creates a new DataGrid with a context menu for the column headers-->
    <DataGrid Name="DG1" ItemsSource="{Binding}"  >
        <DataGrid.ColumnHeaderStyle >
            <Style TargetType="DataGridColumnHeader">
                <Setter Property="ContextMenu" Value="{StaticResource ColumnHeaderMenu}" />
            </Style>
        </DataGrid.ColumnHeaderStyle>
    </DataGrid>
</Grid>
Class Window1
    Public Shared FreezeColumnCommand As New RoutedUICommand()

    Public Sub New()
        InitializeComponent()

        'GetData connects to the database and returns the data in a table. 
        Dim dt As AdventureWorksLT2008DataSet.SalesOrderDetailDataTable = GetData()

        DG1.DataContext = dt
    End Sub


...


Private Sub CommandBinding_Executed(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
    'Get the column header that started the command and move that column left to freeze it. 
    Dim header As System.Windows.Controls.Primitives.DataGridColumnHeader = DirectCast(e.OriginalSource, System.Windows.Controls.Primitives.DataGridColumnHeader)
    If header.Column.IsFrozen = True Then 
        Exit Sub 
    Else
        header.Column.DisplayIndex = DG1.FrozenColumnCount
        DG1.FrozenColumnCount += 1


    End If 
End Sub

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4
Show:
© 2015 Microsoft