Through a grid's Columns collection, it is possible to access all the Columns in a grid, regardless of their visibility. To access only those that are 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 said 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 said positions can still be made.
The MinWidth and MaxWidth properties can be used to specify a Column's Width. 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 is fixed. The actual Width can be retrieved through the ActualWidth property, which returns a value taking the Width, MaxWidth, and MinWidth properties into consideration. The fitted Width of a Column represents the Width necessary to display the entire content of the Cells on one line and can be retrieved through the GetFittedWidth method whose value can then be assigned to the Width property.
If the total Width of all the Columns is less than the viewport's, 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 first Column's Width will reduce until it hits 150, at which point the value will remain at 150 and the horizontal scroll bar will appear (see Example 1 below for more information).
When Column stretching is not enabled (ColumnStretchMode property set to None), a Star (*) unit type, which allows the width of a Column 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). Setting a starred value with a ColumnStretchMode other than None results 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 AllowColumnResize property of any ColumnManagerRows to false. By default, Columns that are affected by the ColumnStretchMode property cannot be resized.
Regardless of how the Width of a Column 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 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 for more information).
Fixed Columns are separated from their scrollable counterparts by a fixed Column splitter.
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; | |