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

Group label format

I have one grid that has one datetime column. 
When I try to use this column, to group itens related, this is showing date in format "dd/MM/yyyy hh:mm:ss AM/PM" in the group label.
I want to be able to format these values acording to my users (MMM-yy) and make syncfusion group itens in the same month/year.

(I tried to create one adictional column of type string that contais that value, but the problem is that the sort is messed up.

Thanks

Attachment: print_186e85ce.zip

3 Replies

SA Solai A L Syncfusion Team June 9, 2015 05:31 AM UTC

Hi Felipe,

Thank you for using Syncfusion products.

To change the value of Caption label after grouping, you can use QueryCellInfo event. In this event you can customize the Caption Row cellvalue as per you requirement. The format for column will be applied only when the cellvaluetype is in corresponding type. Please refer the below image for reference. Also the below code and sample for further clarification. If your requirement is different from this please let us know with a brief explanation.

Code Snippet[c#]:

this.gridGroupingControl1.TableDescriptor.Columns["Date"].Appearance.AnyCell.CellValueType = typeof(DateTime);

this.gridGroupingControl1.TableDescriptor.Columns["Date"].Appearance.AnyCell.Format = "MMM-yyyy";

this.gridGroupingControl1.QueryCellStyleInfo += new GridTableCellStyleInfoEventHandler(gridGroupingControl1_QueryCellStyleInfo);

  void gridGroupingControl1_QueryCellStyleInfo(object sender, GridTableCellStyleInfoEventArgs e)

        {

            if (e.TableCellIdentity.TableCellType == GridTableCellType.GroupCaptionCell && e.TableCellIdentity.GroupedColumn != null)

            {

                GridCaptionRow captionRow = e.TableCellIdentity.DisplayElement as GridCaptionRow;

                string date="";

                DateTime dt;

                if (e.Style.Text != null && DateTime.TryParse(e.Style.Text.Substring(e.Style.Text.IndexOf(":") + 1, 8),out dt))

                {

                    date = dt.ToString("MMM-yyyy");

                }

                e.Style.CellValue = String.Format("{0}: {1} Items.", date, e.TableCellIdentity.DisplayElement.ParentGroup.GetChildCount());


            }

        }
Sample:
https://www.syncfusion.com/downloads/support/directtrac/general/ze/WindowsFormsApplication21_(7)-3092278901853385473.zip



Thanks & Regards,
AL.Solai



FB Felipe Bahiana Almeida June 9, 2015 12:33 PM UTC

Thanks Solai, your example is good but there is one part missing.
Note that the sort order is wrong. We must see dates in this format but order by the Date values, not by string values.



SA Solai A L Syncfusion Team June 10, 2015 11:55 AM UTC

Hi Felipe,


Sorry for the inconvenience.


By default our grid grouping control support only string sorting. For your case you need to use Custom sorting using IComparer and handle that in SortedColumns.Changing event with the following code.  Please refer the below sample and code snippet for further clarification.

Code Snippet[c#]:

gridGroupingControl1.TableDescriptor.SortedColumns.Changing += new Syncfusion.Collections.ListPropertyChangedEventHandler(SortedColumns_Changing);


void SortedColumns_Changing(object sender, Syncfusion.Collections.ListPropertyChangedEventArgs e)

        {

            SortColumnDescriptor sd = e.Item as SortColumnDescriptor;

            if (sd != null && e.Action == Syncfusion.Collections.ListPropertyChangedType.Add)

            {

                Type type = this.gridGroupingControl1.TableDescriptor.Columns[sd.Name].Appearance.AnyRecordFieldCell.CellValueType;

                if (sd.Name == "Date" && type == typeof(DateTime))

                    sd.Comparer = new DateComparer();


            }

        }



public class DateComparer : IComparer

    {

        public DateComparer() { }


        public int Compare(object x, object y)

        {

            DateTime value1 = DateTime.Parse(x.ToString());

            DateTime value2 = DateTime.Parse(y.ToString());


            return DateTime.Compare(value1,value2);

        }

    }


Sample:

https://www.syncfusion.com/downloads/support/directtrac/general/ze/WindowsFormsApplication21_(8)-983861376-1706966913.zip


Please let us know if you have any other concerns.


Thanks & Regards,

AL.Solai.


Loader.
Live Chat Icon For mobile
Up arrow icon