Grouping on JSON data

1 Answer 156 Views
GridView
Paul
Top achievements
Rank 1
Paul asked on 13 Oct 2022, 07:45 PM

When doing grouping on a column who's cells contain JSON formatted data, the application crashes.

The cells contain the following example:

{"Field1":"Data1","Filed2":"Data2"}

Is there a way to handle this interaction?

 

1 Answer, 1 is accepted

Sort by
1
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 14 Oct 2022, 09:20 AM
Hello, Paul,  

According to the provided information, it is not clear what is the column type for this particular column. Is it a GridViewTextBoxColumn? If yes, usually this expects text value and the grouping is performed over the string values.

However, for any unsupported format, it is suitable to use the custom grouping functionality which is a flexible mechanism for creating groups by using custom logic. More information is available here: https://docs.telerik.com/devtools/winforms/controls/gridview/grouping/custom-grouping 

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Paul
Top achievements
Rank 1
commented on 14 Oct 2022, 01:39 PM

It is a GridViewTextBoxColumn containing the JSON formatted data. I'm not sure where the exception is produced, but the message results "Input string was not in a correct format".  Because my RadGridView takes in generic data, I do not know which column will have JSON formatted data prior. 

I can provide a copy of the StackTrace

Dess | Tech Support Engineer, Principal
Telerik team
commented on 14 Oct 2022, 01:42 PM

Paul, the stack trace will be definitely helpful. However, it would be greatly appreciated if you can provide a complete sample code snippet that we can put inside a new project and replicate the undesired behavior on our end. Once, we replicate the issue locally, we would be able to make an adequate analysis of the precise case and provide further assistance. Thank you in advance.
Paul
Top achievements
Rank 1
commented on 18 Oct 2022, 09:04 PM

I had done some extra work last week in other areas in the app and without knowing, I had fixed the issue. 

However, thank you for taking the time to assist.

Paul
Top achievements
Rank 1
commented on 19 Oct 2022, 01:51 PM | edited

Correction: I had inadvertently found a workaround to the issue by disabling radGridView1.EnableCustomGrouping.

To better explain what I am attempting to do, I am taking whatever is in the data cell into the GroupHeader when grouping is triggered. I believe the error stems from the fact that e.FormatString is not escaping characters of the JSON formatted data, that is the quotes in this example are breaking e.FormatString. I'm setting the GroupHeader with this line:

   e.FormatString = String.Format("{0} - Item Count: {1}", e.Value, _rowCount);
Where e.Value = 
{"Field1":"Data1","Filed2":"Data2"}

 

Paul
Top achievements
Rank 1
commented on 19 Oct 2022, 02:31 PM | edited

Sorry for the quick response, but I found that String.Format has a problem { } characters. Is there a for C# to escape all characters ?

I've attached a copy of a small project to demonstrate the issue

Dess | Tech Support Engineer, Principal
Telerik team
commented on 21 Oct 2022, 07:19 AM

 Hello, Paul,   


The provided sample project is greatly appreciated. It seems that the problem occurs in the String.Format function and the passed JSON content to it. Formatting a string is more like a general programming question. That is why after some research in general programming forums I have found the following thread which seems to be useful on this topic:
https://stackoverflow.com/questions/91362/how-to-escape-braces-curly-brackets-in-a-format-string-in-net

I tested a similar approach on my end and it seems to work as expected now:

            string value = "{RunFunc\":\"RunJob\"}";
            int _rowCount = 10;
            string test = String.Format("{0} - Item Count: {1}", value, _rowCount);

Note that the curly brackets from the JSON are confusing since they participate in the place holders for the Format.String. The possible solution that I can suggest is to use the custom grouping approach. I have prepared a sample code snippet for your reference:

 

            this.radGridView1.EnableCustomGrouping = true;
            this.radGridView1.CustomGrouping += RadGridView1_CustomGrouping;       

        private void RadGridView1_CustomGrouping(object sender, GridViewCustomGroupingEventArgs e)
        {
            string data = e.Row.Cells["Data"].Value.ToString();

            e.GroupKey =string.Format("{{{0}}}", data);
            e.Handled = true;
        }

        private void RadGridView1_GroupSummaryEvaluate(object sender, GroupSummaryEvaluationEventArgs e)
        { 
            try
            {
                int _rowCount = 0;
                foreach (GridViewRowInfo row in e.Group)
                {
                    _rowCount++;
                }
                e.FormatString = e.Group.Key.ToString() + " - Item Count: " + _rowCount;
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Uncaught error in {System.Reflection.MethodBase.GetCurrentMethod().Name}: \n{ex.Message}", caption: "Error");
            }
        }

Please give it a try and see how it works on your end.

Paul
Top achievements
Rank 1
commented on 24 Oct 2022, 04:39 PM

Thank you for your in-depth response, Dess. I maybe able to implement this solution but the problem I face now is I do not know the data  and columns of the GridView before hand, meaning I cannot specify which column will need special formatting when doing 
string data = e.Row.Cells[ColumnName].Value.ToString();
Dess | Tech Support Engineer, Principal
Telerik team
commented on 25 Oct 2022, 01:00 PM

Paul, the JSON content and its curly brackets "{}" inside the grid cells confuses the FormatString pattern as it also uses these brackets. That is why if it is difficult to  specify the precise column and implement the custom grouping, an easier approach is to replace the curly brackets "{}" with other or empty symbols so the formatting would run smoothly. It is up to you which approach to follow in order to achieve the custom requirement you have.
Tags
GridView
Asked by
Paul
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or