This is a migrated thread and some comments may be shown as answers.

Format GroupByExpessions Sum Total

3 Answers 192 Views
GridView
This is a migrated thread and some comments may be shown as answers.
junk smith
Top achievements
Rank 1
junk smith asked on 14 Aug 2009, 03:48 PM
Hello Again


Using

.MasterGridViewTemplate.GroupByExpressions.Add("Application,sum(Score) As [Sub Total Score = ] Group By Application")

which works fine but on multiple applicaion rows is really messy, is there a way to format the heading to create an easy to read column, so instead of something that looks like this;

app1firstappname;Sub Total Score = 2
app2secondappname;Sub Total Score = 3
app2thirdappname;Sub Total Score = 3

it would more look like this

app1firstappname;                                        Sub Total Score = 2
app2secondappname;                                   Sub Total Score = 3
app2thirdappname;                                       Sub Total Score = 3







Cheers

3 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 17 Aug 2009, 07:23 AM
Hello junk smith,

Yes, you can handle GroupSummaryEvaluate event in this case. Please look at the following blog article. I hope it helps. If you have any questions, don't hesitate to write back.

All the best,
Jack
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Versile
Top achievements
Rank 1
answered on 21 Aug 2009, 05:07 AM
After 6 hours studying this topic I want to elaborate on my experience and hopefully help others. My goal was to perfectly align the text I created on my grouping headers and format that text as I am of the school everything I create should look perfect and that is what attracted me to Telerik over the competition. Unfortunately this has sometimes been difficult, but so far mostly doable.

A) The GroupSumaryEvaluate event does not require you to use the more complex SummaryRowGroupHeaders[0] syntax. You can create all your group by expressions as normal (as shown in the below code) then add this event and handle each grouping individually. The attached blog post isn't really easy to read but I would have not figured this out if it weren't for that so a lot of thanks to Tsvetan Raykov.

B) The format string expressions do not fully work as I expected (i.e. you can also consider this a bug report) 
For Example:  {0,3} should ensure that variable 0 is always 3 characters in length. A Positive number after the comman should indicate they will align on the right of the character string, negative would indicate they align on the left. In the gridviewgroupbyexpression they simply add spaces to the end of the string period. So I needed a workaround.

C) The ViewCellElement and ViewRowElement events would not fire until my mouse was over them, and then would fire every time my mouse went over them, causing undesirable behaviour in addition to not working as I intended. I gave up on this method flatly after 5 hours of work. I spent less than a hour figuring out the GroupSumaryEvaluate event (btw Sumary is spelled Summary?) I was also unable to get the IsCurrent evaluation of the ViewCellElement to read properly.

So my code (note I added a lot of other methods in here such as expanding only certain rows, swapping the "datafield" (FieldName) for specific columns, formatting entire rows (highlight rows) that is already part of my code and if it helps someone more power to you.

        private void build_grid() 
        { 
            this.Cursor = Cursors.WaitCursor; 
            // Finds the current value based on primary Key for my datasource, using some 
            // customer navigation elements to traverse a datatable, up adds one to currRow, - removes one from currRow 
            // so the find function returns the row with the ID listed 
            DataRow wRow = wTable.Rows.Find(currRow);  
            weekOf_lbl.Text = wRow["weekOf"].ToString(); 
 
            // WebService to fill dataset, _locationID is from a radcombobox item event 
            QueryResult qInfo = 
                schedulingClient.SchedulingService.Get_Cal_MA(_locationID, ((DateTime)wRow["startDate"]).Date, ((DateTime)wRow["endDate"]).Date); 
            if (!qInfo.Success) 
            { 
                Telerik.WinControls.RadMessageBox.Show(qInfo.Message, "Failed to Retrieve Calendar Information"); 
                this.Cursor = Cursors.Default; 
                return
            } 
            // I use one grid for two sets of values, a radgroupbox toggles the two values 
            // with radio buttons inside by modifying the field name only 
            if (radio_jobs.ToggleState == Telerik.WinControls.Enumerations.ToggleState.On) 
            { 
                this.calendar_grida.Columns["max"].FieldName = "maxjobs"
                this.calendar_grida.Columns["current"].FieldName = "currjobs"
                this.calendar_grida.Columns["available"].FieldName = "availjobs"
                this.calendar_grida.Columns["percentage"].FieldName = "jobPercentage"
            } 
            else 
            { 
                this.calendar_grida.Columns["max"].FieldName = "maxunits"
                this.calendar_grida.Columns["current"].FieldName = "currunits"
                this.calendar_grida.Columns["available"].FieldName = "availunits"
                this.calendar_grida.Columns["percentage"].FieldName = "unitPercentage"
            } 
            // Ensure when the grouping happens they are in the appropriate order 
            qInfo.Ds.Tables[0].DefaultView.Sort = "weekOf, installDate, MA"
            this.calendar_grida.DataSource = qInfo.Ds.Tables[0]; 
 
            // Setup automatic Grouping of WeekOf Date 
            this.calendar_grida.MasterGridViewTemplate.GroupByExpressions.Add( 
                "[weekOf] as [Week Of] format \"{0}:  {1:d}\" Group By [weekOf]"); 
            // Setup grouping with summary of the values the grid was created for 
            string expression =  
                "[dayofWeek] as [Day Of Week] format \"{1}- \", " + 
                "[installDate] as [Insall Date] format \"{1:d}  \", " + 
                "sum(max) as [Max] format \"{0} / {1} \", " + 
                "sum(current) as [Current] format \"{0} / {1} \", " + 
                "sum(available) as [Available] format \"{0} / {1}\" " + 
                "Group By [installDate]"
            this.calendar_grida.MasterGridViewTemplate.GroupByExpressions.Add(expression); 
            // Expand all groups by default 
            this.calendar_grida.MasterGridViewTemplate.ExpandAllGroups(); 
            // Color rows by Percentage of capacity (one of my values) 
            Telerik.WinControls.UI.ConditionalFormattingObject[] obj = 
                new ConditionalFormattingObject[calendar_grida.Rows.Count]; 
            // one for each row for the formatting object 
            int counter = 0; 
            // one for each groupheader 
            int dayCounter = 0; 
            // Use this value to only add 1 to the dayCounter when the date changes 
            DateTime dt = ((DateTime)wRow["startDate"]).Date; 
            // Ensure formatting is cleared before we start 
            this.calendar_grida.Columns["id"].ConditionalFormattingObjectList.Clear(); 
 
            foreach (Telerik.WinControls.UI.GridViewDataRowInfo row in this.calendar_grida.Rows) 
            { 
                double percentage = Convert.ToDouble(row.Cells["percentage"].Value); 
                // Start row coloring process by setting the Select object 
                obj[counter] = new Telerik.WinControls.UI.ConditionalFormattingObject("SelectCondition"
                Telerik.WinControls.UI.ConditionTypes.Contains, row.Cells["id"].Value.ToString(), ""true); 
                // Turn all rows green to start with 
                obj[counter].RowBackColor = Color.LightGreen; 
                // Add a day to my day counter (I want to intentionally skip the first one as it's my weekof header 
                if (((DateTime)row.Cells["installDate"].Value).Date > dt) 
                { 
                    dayCounter++; 
                    dt = dt.AddDays(1); 
                } 
                if ((DateTime)row.Cells["installDate"].Value > DateTime.Now) 
                { 
                    // Expand the row IF it's greater than today 
                    row.Group.Groups[dayCounter].HeaderRow.IsExpanded = true
                    if (percentage >= .6) // Indicate middle tier (yellow) 
                    { 
                        obj[counter].RowBackColor = Color.Yellow; 
                    } 
                    if (percentage >= .8) // Indicate upper tier (red) 
                    { 
                        obj[counter].RowBackColor = Color.Red; 
                    } 
                    if (percentage > 1.0) // Indicate overbooked (violet) 
                    { 
                        obj[counter].RowBackColor = Color.Violet; 
                    } 
                } 
                else 
                { 
                    // Hide Row if it's less than or equal to today 
                    row.Group.Groups[dayCounter].HeaderRow.IsExpanded = false
                    // Turn all rows Antique White if less than or equal to today 
                    obj[counter].RowBackColor = Color.AntiqueWhite; 
                } 
                // Apply the color 
                this.calendar_grida.Columns["id"].ConditionalFormattingObjectList.Add(obj[counter]); 
                counter++; 
            } 
            // Kick off an event to customize the formatting of the group headers 
            this.calendar_grida.GroupSumaryEvaluate += new GroupSummaryEvaluateEventHandler(calendar_grida_GroupSumaryEvaluate); 
            this.Cursor = Cursors.Default; 
        } 

And my groupsumaryevaluate event

        void calendar_grida_GroupSumaryEvaluate(object sender, GroupSummaryEvaluationEventArgs e) 
        { 
            // Need a fixed width font for perfect display of values 
            e.Group.HeaderRow.GridViewInfo.GridViewElement.Font = 
                new Font("Courier New", e.Group.HeaderRow.GridViewInfo.GridViewElement.Font.Size); 
            int i = 0; 
            // Days of week maximum width is 9 (Wednesday) so if it's less than 9 add a space 
            if (e.SummaryItem.FieldName == "dayofWeek"
            { 
                i = 0; 
                while (i <9) 
                { 
                    i = e.Value.ToString().Length; 
                    e.Value = e.Value + " "
                } 
            } 
            // Set default length of my summary items for up to 999 to display perfectly 
            int length = 3; 
            // If I'm using the larger calculation set the length to 5 to allow up to 99999 to display perfectly 
            if (radio_units.ToggleState == Telerik.WinControls.Enumerations.ToggleState.On) 
            { 
                length = 5; 
            } 
            // Since the values are all the same general vicinity use one if statement for all 3 
            if (e.SummaryItem.FieldName == "max" | 
                e.SummaryItem.FieldName == "available" | 
                e.SummaryItem.FieldName == "current"
            { 
                i = e.Value.ToString().Length; 
                while (i < length) 
                { 
                    e.Value = " " + e.Value; 
                    i = e.Value.ToString().Length; 
                } 
            } 
        } 

Thanks,

Versile

0
Nick
Telerik team
answered on 21 Aug 2009, 08:32 AM
Hi Versile,

Thank you for sharing your code with the community. I have updated your Telerik points.

Sincerely yours,
Nick
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
GridView
Asked by
junk smith
Top achievements
Rank 1
Answers by
Jack
Telerik team
Versile
Top achievements
Rank 1
Nick
Telerik team
Share this question
or