Xceed Toolkit Plus for WPF v4.6 Documentation
Column Class
Members  Example 


Xceed.Wpf.DataGrid Assembly > Xceed.Wpf.DataGrid Namespace : Column Class
Represents a column, which defines information on how the Cells it contains are displayed and their content edited.
Syntax
'Declaration
 
<DebuggerDisplayAttribute("FieldName = {FieldName}")>
<TypeDescriptionProviderAttribute(MS.Internal.ComponentModel.DependencyObjectProvider)>
<NameScopePropertyAttribute("NameScope", System.Windows.NameScope)>
Public Class Column 
   Inherits ColumnBase
'Usage
 
Dim instance As Column
[DebuggerDisplay("FieldName = {FieldName}")]
[TypeDescriptionProvider(MS.Internal.ComponentModel.DependencyObjectProvider)]
[NameScopeProperty("NameScope", System.Windows.NameScope)]
public class Column : ColumnBase 
Remarks

The VisiblePosition property represents the visible position of the column in a grid and may, or may not, correspond to the Index property. A column's visible position can be regarded as the position the column will take when it is visible. Changing a column's visibility will not affect its visible position, allowing the visibility of columns to be changed without them being reordered. To retrieve the effective visible position of a column, the IndexOf method of the VisibleColumns collection can be used.

When fixing columns in a table-view layout, columns will be fixed in the order represented by their VisiblePosition properties. For example, if the FixedColumnCount property is set to 2, then the columns whose VisiblePosition properties are 0 and 1 will be fixed.

If a grid contains hidden columns, they will be ignored. For example, if the second and third columns are hidden and a fixed-column count of 2 is provided, the first and fourth columns will be fixed but their visible positions will not be modified to reflect their new positions.

Example

All examples in this topic assume that the grid is bound to the Orders table of the Northwind database, unless stated otherwise.

The following example demonstrates how to provide a custom sort comparer that sorts addresses. The AddressComparer class (provided below) will first sort addresses which begin with numeric values by street name and then civic number. Address that do not have a civic number will be sorted alphabetically.
<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
      xmlns:local="clr-namespace:Xceed.Wpf.Documentation">
   <Grid.Resources>
      <local:AddressComparer x:Key="addressComparer"/>
      <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="ShipAddress"
                                       SortComparer="{StaticResource addressComparer}"/>
            <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="StretchAll"/>
      </xcdg:DataGridControl.View>
   </xcdg:DataGridControl>
</Grid>
The following code provides the implementation of the AddressComparer class.
Imports System
Imports System.Collections
Imports System.Data
Namespace Xceed.Wpf.Documentation

  Public Class AddressComparer
               Implements IComparer

    Public Sub New()
    End Sub
    Public Function Compare( x As Object, y As Object ) As Integer Implements IComparer.Compare
      Dim stringX As String = CType( x, String )
      Dim stringY As String = Ctyle( y, String )
      Const digits As String = "0123456789"
      If( ( digits.IndexOf( stringX( 0 ) ) >= 0 ) And ( digits.IndexOf( stringY( 0 ) ) >= 0 ) ) Then
        Dim index As Integer = 0
        Dim xNumber As System.Text.StringBuilder = New System.Text.StringBuilder()
        While( ( index < stringX.Length ) And ( digits.IndexOf( stringX( index ) ) >= 0 ) )
          xNumber.Append( stringX( index ) )
          index++
        End While
        index = 0
        Dim yNumber As System.Text.StringBuilder = New System.Text.StringBuilder()
        While( ( index < stringY.Length ) And ( digits.IndexOf( stringY( index ) ) >= 0 ) )
          yNumber.Append( stringY( index ) )
          index++
        End While
        Dim xValue = Long.Parse( xNumber.ToString() )
        Dim yValue As Long = Long.Parse( yNumber.ToString() )
        If( xValue > yValue ) Then
          Return 1
        End If
        If( xValue < yValue ) Then
          Return -1
        End If
        Return stringX.CompareTo( stringY )
      Else
        Return stringX.CompareTo( stringY )
      End If
    End Function
  End Class
End Namespace
The following code provides the implementation of the AddressComparer class.
using System;
using System.Collections;
using System.Data;
namespace Xceed.Wpf.Documentation
{
 public class AddressComparer: IComparer
 {
   public AddressComparer()
   {
   }

   int IComparer.Compare( object x, object y )
   {
     string stringX = ( string )x;
     string stringY = ( string )y;

     const string digits = "0123456789";
     if( ( digits.IndexOf( stringX[ 0 ] ) >= 0 ) && ( digits.IndexOf( stringY[ 0 ] ) >= 0 ) )
     {
       int index = 0;
       System.Text.StringBuilder xNumber = new System.Text.StringBuilder();

       while( ( index < stringX.Length ) && ( digits.IndexOf( stringX[ index ] ) >= 0 ) )
       {
         xNumber.Append( stringX[ index ] );
         index++;
       }

       index = 0;
       System.Text.StringBuilder yNumber = new System.Text.StringBuilder();

       while( ( index < stringY.Length ) && ( digits.IndexOf( stringY[ index ] ) >= 0 ) )
       {
         yNumber.Append( stringY[ index ] );
         index++;
       }

       long xValue = long.Parse( xNumber.ToString() );
       long yValue = long.Parse( yNumber.ToString() );

       if( xValue > yValue )
         return 1;

       if( xValue < yValue )
         return -1;

       return stringX.CompareTo( stringY );
     }
     else
     {
       return stringX.CompareTo( stringY );
     }
   }
 }
}
Inheritance Hierarchy

System.Object
   System.Windows.Threading.DispatcherObject
      System.Windows.DependencyObject
         System.Windows.Freezable
            Xceed.Wpf.DataGrid.ColumnBase
               Xceed.Wpf.DataGrid.Column

Requirements

Target Platforms: Windows 11, Windows 10, Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

Column Members
Xceed.Wpf.DataGrid Namespace

DataGrid Fundamentals

Column_Class.html