Through a grid's Columns collection, said Columns can be accessed, regardless of their visibility. To access only the ones visible—which are those whose Visible property is set to true—the VisibleColumns property can be consulted. The value of a Column's Index property represents its position within the collection, while the visible position of a Column can be retrieved through its VisiblePosition property, which may or may not correspond to the Index property. In addition to the VisiblePosition property, the IsFirstVisible and IsLastVisible properties can be used to determine if a Column is the first or last one visible in a grid. A ColumnManagerRow's AllowColumnReorder property can prevent end users from changing the visible positions of Columns; however, programmatic changes to their VisiblePosition can still be made.
The MinWidth and MaxWidth properties can be used to specify a Column's Width. However, in order for a Column to have a fixed Width, the MinWidth and MaxWidth properties must be set to the same value. The HasFixedWidth property can be consulted to determine if the Width of a Column is fixed. The actual Width can be retrieved through the ActualWidth property, which takes the Width, MaxWidth, and MinWidth properties into consideration . The fitted Width represents the Width necessary to display the entire Cells' content on one line and can be retrieved through the GetFittedWidth method, whose value can then be assigned to the Width property.
In a TableView layout, if the total Width of all the Columns is less than the width of the viewport, the Width of the first or last Columns, or of all the Columns, can be stretched to occupy the available viewport's width by setting the TableView's ColumnStretchMode property. The ColumnStretchMinWidth property determines the minimum size that a stretched Column (First, Last, or All) can have. For example, if the ColumnStretchMinWidth property is set to 150 (by default 50d) and the ColumnStretchMode property set to First, when the containing grid is resized, the Width of the first Column will reduce until it hits 150, at which point it will remain at 150 and the horizontal scroll bar will appear (see Example 1 at the bottom of the page).
When Column stretching is not enabled (ColumnStretchMode property set to None), a Star (*) unit type, which allows the Column's Width to be based on a weighted proportion in regards to available space rather than an explicit pixel value, can also be provided to the Width property (see the ColumnWidthUnitType enumeration for more information). Note that setting a starred value with a ColumnStretchMode other than None will result in the default width of 125d being used.
It is highly recommended to set the MinWidth property of a Column whose Width property is set to a starred (*) value as well as set the ColumnManagerRows' AllowColumnResize property to false. Note that by default, Columns that are affected by the ColumnStretchMode property cannot be resized.
Regardless of how a Column's Width is provided (e.g., starred value, stretch last Column), it will always be limited by the values of the MinWidth and MaxWidth properties.
When a grid is in a TableView layout, the first n Columns can be fixed so that they do not scroll with the grid's content. Columns are fixed according to the value of their VisiblePosition property (ignoring hidden Columns) and the value of the TableView's FixedColumnCount property, which indicates how many Columns are fixed, in the order dictated by their VisiblePosition (see Example 2 below).
Fixed Columns are separated from their scrollable counterparts by a fixed Column splitter, which can be dragged to add or remove fixed Columns. Likewise, ColumnManagerCells can be dragged to the left or right of the fixed Column splitter to add or remove fixed Columns.
All examples in this topic assume that the grid is bound to the Orders table of the Northwind database, unless stated otherwise.
Example 1: Stretching columns
The following example demonstrates how to stretch all the columns in a grid equally so that they occupy the full width available in the viewport.
| XAML |
Copy Code |
|---|---|
<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"> <Grid.Resources> <xcdg:DataGridCollectionViewSource x:Key="cvs_orders" Source="{Binding Source={x:Static Application.Current}, Path=Orders}" AutoCreateItemProperties="False"> <xcdg:DataGridCollectionViewSource.ItemProperties> <xcdg:DataGridItemProperty Name="ShipCountry" /> <xcdg:DataGridItemProperty Name="ShipCity" /> <xcdg:DataGridItemProperty Name="ShipRegion" /> <xcdg:DataGridItemProperty Name="ShipVia" /> </xcdg:DataGridCollectionViewSource.ItemProperties> </xcdg:DataGridCollectionViewSource> </Grid.Resources> <xcdg:DataGridControl x:Name="OrdersGrid" ItemsSource="{Binding Source={StaticResource cvs_orders}}"> <xcdg:DataGridControl.View> <xcdg:TableView ColumnStretchMode="All" ColumnStretchMinWidth="100"/> </xcdg:DataGridControl.View> </xcdg:DataGridControl> </Grid> | |
| VB.NET |
Copy Code |
|---|---|
Dim collectionView As New DataGridCollectionView( Orders, GetType( System.Data.DataRow ), False, False ) collectionView.ItemProperties.Add( New DataGridItemProperty( "ShipCountry", GetType( String ) ) ) collectionView.ItemProperties.Add( New DataGridItemProperty( "ShipCity", GetType( String ) ) ) collectionView.ItemProperties.Add( New DataGridItemProperty( "ShipRegion", GetType( String ) ) ) collectionView.ItemProperties.Add( New DataGridItemProperty( "ShipVia", GetType( Integer ) ) ) Dim view As New TableView() view.ColumnStretchMode = ColumnStretchMode.All view.ColumnStretchMinWidth = 100 dataGridControl.View = view dataGridControl.ItemsSource = collectionView | |
| C# |
Copy Code |
|---|---|
DataGridCollectionView collectionView = new DataGridCollectionView( Orders, typeof( System.Data.DataRow ), false, false ); collectionView.ItemProperties.Add( new DataGridItemProperty( "ShipCountry", typeof( string ) ) ); collectionView.ItemProperties.Add( new DataGridItemProperty( "ShipCity", typeof( string ) ) ); collectionView.ItemProperties.Add( new DataGridItemProperty( "ShipRegion", typeof( string ) ) ); collectionView.ItemProperties.Add( new DataGridItemProperty( "ShipVia", typeof( int) ) ); TableView view = new TableView(); view.ColumnStretchMode = ColumnStretchMode.All; view.ColumnStretchMinWidth = 100; dataGridControl.View = view; dataGridControl.ItemsSource = collectionView; | |
Example 2: Fixing columns
The following example demonstrates how to fix the ShipCountry and ShipCity columns.
| XAML |
Copy Code |
|---|---|
<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"> <Grid.Resources> <xcdg:DataGridCollectionViewSource x:Key="cvs_orders" Source="{Binding Source={x:Static Application.Current}, Path=Orders}"/> </Grid.Resources> <xcdg:DataGridControl x:Name="OrdersGrid" ItemsSource="{Binding Source={StaticResource cvs_orders}}"> <xcdg:DataGridControl.Columns> <xcdg:Column FieldName="ShipCountry" VisiblePosition="0"/> <xcdg:Column FieldName="ShipCity" VisiblePosition="1"/> </xcdg:DataGridControl.Columns> <xcdg:DataGridControl.View> <xcdg:TableView FixedColumnCount="2"/> </xcdg:DataGridControl.View> </xcdg:DataGridControl> </Grid> | |
| VB.NET |
Copy Code |
|---|---|
dataGridControl.Columns( "ShipCountry" ).VisiblePosition = 0 dataGridControl.Columns( "ShipCity" ).VisiblePosition = 0 Dim view As New TableView() view.FixedColumnCount = 2 dataGridControl.View = view | |
| C# |
Copy Code |
|---|---|
dataGridControl.Columns[ "ShipCountry" ].VisiblePosition = 0; dataGridControl.Columns[ "ShipCity" ].VisiblePosition = 0; TableView view = new TableView(); view.FixedColumnCount = 2; dataGridControl.View = view; | |