We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

TreeGridDateTimeColumn format string to datetime

Hi,

I am bindig a dictionary(of string) to a SfTreeGrid. This works as expected. I am using AutoGenerateColumns="True" because the content of the dictionary is dynamic. 

But there is one column (createdate) that is always a datetime (2022-11-12 09:09:09).
Is there a way to set this one column as 
TreeGridDateTimeColumn, because I need the sort and filter functionality?


Kind regards, Marco


3 Replies

SJ Sathiyathanam Jeyakumar Syncfusion Team November 15, 2022 10:00 AM


You can customize columns creations for dynamic objects by using the AutoGeneratingColumn event. Refer the below the code snippets and UG link to get the more details.


<Syncfusion:SfTreeGrid Grid.Row="1"

    ItemsSource="{Binding Roots}"

    AutoGeneratingColumn="SfTreeGrid_AutoGeneratingColumn">

 

private void SfTreeGrid_AutoGeneratingColumn(object sender, Syncfusion.UI.Xaml.TreeGrid.TreeGridAutoGeneratingColumnEventArgs e)

{

    //Here you can be able to create the TreeGridDateTimeColumn for DateTime data's

    if(e.Column.MappingName == "Your date column type like :) Values['DateKey']")

    {

        e.Column = new TreeGridDateTimeColumn()

        {

            MappingName = e.Column.MappingName,

            DateTimeFormat = DateTimeFormatInfo.CurrentInfo

        };

    }

}


UG Link : https://help.syncfusion.com/wpf/treegrid/columns#customize-auto-generated-columns



MU Marco Uffelmann November 15, 2022 11:55 AM

Hi,

this is not working. Seems that it is still handled as a string and not as a date. Trying to use the date filter (e.g. before) I am getting this error:

The binary operator LessThan is not defined for types "System.String" and "System.String"


This is my code:

Private Sub treeGridSearchResult_AutoGeneratingColumn(sender As Object, e As TreeGridAutoGeneratingColumnEventArgs) Handles treeGridSearchResult.AutoGeneratingColumn
        If e.Column.MappingName = "Erstelldatum" Then

            If TypeOf e.Column Is TreeGridTextColumn Then
                e.Column = New TreeGridDateTimeColumn() With {
                    .MappingName = "Erstelldatum",
                    .DateTimeFormat = DateTimeFormatInfo.CurrentInfo,
                    .Pattern = Syncfusion.Windows.Shared.DateTimePattern.ShortDate
                }
            End If
        End If
    End Sub

Also sorting is not working as you can see in this screenshot. This is the german date format dd.MM.yyyy.

Unbenannt.JPG


Any idea? Thank for your help.



SJ Sathiyathanam Jeyakumar Syncfusion Team November 16, 2022 09:16 AM

You can be able to Sort the string date based on DateTime type by using the below custom sorting. And also, you can change the AdvancedFilterType while open the FilterPopup by using the FilterItemsPopuplating event. Refer the below code snippets. We have attached the sample for your references.


public MainWindow()

{

    InitializeComponent();

    //Used to set the AdvancedFilterType while open the FilterPopup.

    this.sftreeGrid.FilterItemsPopulating += OnSfTreeGridFilterItemsPopulating;

    //CustomSorting used to perform the sorting.

    this.sftreeGrid.SortComparers.Add(new SortComparer() { Comparer = new CustomSortComparer(), PropertyName = "OrderDate" });

 

}

private void OnSfTreeGridFilterItemsPopulating(object sender, TreeGridFilterItemsPopulatingEventArgs e)

{

    if (e.Column.MappingName == "OrderDate")

        e.FilterControl.AdvancedFilterType = AdvancedFilterType.DateFilter;

}

private void dataGrid_AutoGeneratingColumn_1(object sender, TreeGridAutoGeneratingColumnEventArgs e)

{

    if (e.Column.MappingName == "OrderDate")

    {

        e.Column = new TreeGridDateTimeColumn()

        {

            AllowNullValue = true,

            MappingName = "OrderDate",

            Pattern = DateTimePattern.CustomPattern,

            CustomPattern = "dd.MM.yyyy",

            AllowFiltering = true

        };

    }

}

public class CustomSortComparer : IComparer<object>, ISortDirection

{

 

    //Custom Sorting for date.

    public int Compare(object x, object y)

    {

        var item1 = x as OrderInfo;

        var item2 = y as OrderInfo;

        DateTime dateTime1;

        bool isDone = DateTime.TryParse(item1.OrderDate, out dateTime1);

        DateTime dateTime2;

        bool isDone1 = DateTime.TryParse(item2.OrderDate, out dateTime2);

               

        int c = 0;

 

        if (dateTime1 != null && dateTime2 == null)

        {

            c = 1;

        }

 

        else if (dateTime1 == null && dateTime2 != null)

        {

            c = -1;

        }

 

        else if (dateTime1 != null && dateTime2 != null)

        {

            c = DateTime.Compare(dateTime1, dateTime2);

        }

 

        if (SortDirection == ListSortDirection.Descending)

            c = -c;

 

        return c;

    }

 

    //Get or Set the SortDirection value

    private ListSortDirection _SortDirection;

 

    public ListSortDirection SortDirection

    {

        get { return _SortDirection; }

        set { _SortDirection = value; }

    }

}


Refer the below UG to get more details about sorting and Filtering.

https://help.syncfusion.com/wpf/treegrid/sorting#custom-sorting

https://help.syncfusion.com/wpf/treegrid/filtering#loading-text-filter-for-number-or-date-column


Attachment: SfDataGrid_Net_338c3678.zip

Loader.
Live Chat Icon For mobile
Up arrow icon