Xceed Toolkit Plus for WPF v4.6 Documentation
GroupNameFromItem Method
Example 


Xceed.Wpf.DataGrid Assembly > Xceed.Wpf.DataGrid Namespace > DataGridGroupDescription Class : GroupNameFromItem Method
The item whose group name is to be retrieved.
A zero-based value representing the group level.
The System.Globalization.CultureInfo to supply to the converter.
Retrieves the group name for the specified item.
Syntax
'Declaration
 
Public Overrides Function GroupNameFromItem( _
   ByVal item As Object, _
   ByVal level As Integer, _
   ByVal culture As CultureInfo _
) As Object
'Usage
 
Dim instance As DataGridGroupDescription
Dim item As Object
Dim level As Integer
Dim culture As CultureInfo
Dim value As Object
 
value = instance.GroupNameFromItem(item, level, culture)
public override object GroupNameFromItem( 
   object item,
   int level,
   CultureInfo culture
)

Parameters

item
The item whose group name is to be retrieved.
level
A zero-based value representing the group level.
culture
The System.Globalization.CultureInfo to supply to the converter.

Return Value

An object representing the group name for the specified item.
Example
All examples in this topic assume that the grid is bound to the Orders table of the Northwind database, unless stated otherwise.
This example demonstrates how to create a custom group description by deriving from the DataGridGroupDescription class and overriding the GroupNameFromItem method. The custom group description will group items according to the first letter in the value received as a parameter. The example results in the group being present at initial loading; also, when removing and re-adding the group, the custom group description is not triggered. See below for an alternative approach to avoid this. The implementation for the custom sort comparer assigned to the group description's SortComparer property is provided below.
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Xceed.Wpf.DataGrid
Imports System.Collections
Imports System.Globalization;

Namespace Xceed.Wpf.Documentation
  Public Class AlphabeticalGroupDescription

         Inherits DataGridGroupDescription
    Public Sub New()
      MyBase.New()
    End Sub

    Public Sub New(ByVal propertyName As String)
     MyBase.New(propertyName)
    End Sub
    Public Overrides Function GroupNameFromItem(ByVal item As Object, _
                                                ByVal level As Integer, _
                                                ByVal culture As CultureInfo) As Object
      Dim value As Object = MyBase.GroupNameFromItem(item, level, culture)
      Try
        Dim content As String = Convert.ToString(value)
        value = content.ToUpper().Substring(0, 1)
      Catch e1 As InvalidCastException
      End Try

      Return value
    End Function
  End Class
End Namespace
The following code provides the implementation for the custom sort comparer that is used to sort, by vowels then consonants, the group descriptions create above.
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Collections

Namespace Xceed.Wpf.Documentation
  Public Class ConsonantVowelComparer

     Implements IComparer
    Public Sub New()
    End Sub


    Public Function Compare(ByVal x As Object, _
                            ByVal y As Object) As Integer Implements IComparer.Compare
      If (Typeof x Is String) AndAlso (Typeof y Is String) Then
        Dim xString As String = x.ToString().ToLowerInvariant()
        Dim yString As String = y.ToString().ToLowerInvariant()
        Dim isXVowel As Boolean = m_vowels.Contains(xString)
        Dim isYVowel As Boolean = m_vowels.Contains(yString)

        If isXVowel Xor isYVowel Then
          If isXVowel Then
            Return -1
          Else
            Return 1
          End If
        End If
        Return String.Compare(xString, yString)
      End If
      Throw New ArgumentException()
    End Function
    Private Const m_vowels As String = "aeiouy"
  End Class
End Namespace
This example demonstrates how to create a custom group description by deriving from the DataGridGroupDescription class and overriding the GroupNameFromItem method. The custom group description will group items according to the first letter in the value received as a parameter. The example results in the group being present at initial loading; also, when removing and re-adding the group, the custom group description is not triggered. See below for an alternative approach to avoid this. The implementation for the custom sort comparer assigned to the group description's SortComparer property is provided below.
using System;
using System.Collections.Generic;
using System.Text;
using Xceed.Wpf.DataGrid;
using System.Collections;

namespace Xceed.Wpf.Documentation
{

  public class AlphabeticalGroupDescription : DataGridGroupDescription
  {
    public AlphabeticalGroupDescription()
      : base()
    {
    }

    public AlphabeticalGroupDescription( string propertyName )
      : base( propertyName )
    {
    }
   

    public override object GroupNameFromItem( object item, int level,
                                              System.Globalization.CultureInfo culture )
    {
      object value = base.GroupNameFromItem( item, level, culture );
      try
      {
        string content = Convert.ToString( value );
        value = content.ToUpper().Substring( 0, 1 );

      }
      catch( InvalidCastException )
      {
      }
      return value;
    }
  }
}
The following code provides the implementation for the custom sort comparer that is used to sort, by vowels then consonants, the group descriptions create above.
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Xceed.Wpf.Documentation
{
  public class ConsonantVowelComparer : IComparer
  {
    public ConsonantVowelComparer()
    {
    }
    public int Compare( object x, object y )
    {
      if( ( x is string ) && ( y is string ) )
      {
        string xString = x.ToString().ToLowerInvariant();
        string yString = y.ToString().ToLowerInvariant();
        bool isXVowel = m_vowels.Contains( xString );
        bool isYVowel = m_vowels.Contains( yString );
        if( isXVowel ^ isYVowel )
          return isXVowel ? -1 : 1;
        return String.Compare( xString, yString );       
      }

      throw new ArgumentException();
    }
    private const string m_vowels = "aeiouy";
  }
}
The following code demonstrates how to use the custom group description by adding it to the DataGridCollectionViewSource's GroupDescriptions property.
<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
     xmlns:local="clr-namespace:Xceed.Wpf.Documentation">
  <Grid.Resources>      
    <local:ConsonantVowelComparer x:Key="consonantVowelComparer"/>

    <xcdg:DataGridCollectionViewSource x:Key="cvs_orders"
                                       Source="{Binding
                                                Source={x:Static Application.Current},
                                                Path=Orders}">

     <xcdg:DataGridCollectionViewSource.GroupDescriptions>
       <local:AlphabeticalGroupDescription PropertyName="ShipCountry"
                                SortComparer="{StaticResource consonantVowelComparer}"/>
     </xcdg:DataGridCollectionViewSource.GroupDescriptions>
    </xcdg:DataGridCollectionViewSource>
  </Grid.Resources>
  <xcdg:DataGridControl x:Name="OrdersGrid"
                        ItemsSource="{Binding Source={StaticResource cvs_orders}}"/>
</Grid>
The first example results in the group being present at initial loading; also, when removing and re-adding the group, the custom GroupDescription is not triggered. But by adding the custom GroupDescription directly to the Column, data is not grouped until the end-user drags the column to create the group, and this occurs each time the end-user performs the operation.
<Window.Resources>
    <local:DateGroupDescription x:Key="myDateGroupDescription"
                                PropertyName="DateTimeFieldName" />
</Window.Resources>

 

[...]

 

<xcdg:DataGridControl.Columns>
  <xcdg:Column FieldName="DateTimeFieldName"
              GroupDescription="{StaticResource myDateGroupDescription}" />
</xcdg:DataGridControl.Columns>
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

DataGridGroupDescription Class
DataGridGroupDescription Members
Base Implementation in GroupNameFromItem