Format GroupByExpessions Sum Total

4 posts, 0 answers
  1. junk smith
    junk smith avatar
    4 posts
    Member since:
    Aug 2009

    Posted 14 Aug 2009 Link to this post

    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
  2. Jack
    Admin
    Jack avatar
    2335 posts

    Posted 17 Aug 2009 Link to this post

    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.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Versile
    Versile avatar
    20 posts
    Member since:
    May 2009

    Posted 21 Aug 2009 Link to this post

    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.

    1         private void build_grid() 
    2         { 
    3             this.Cursor = Cursors.WaitCursor; 
    4             // Finds the current value based on primary Key for my datasource, using some 
    5             // customer navigation elements to traverse a datatable, up adds one to currRow, - removes one from currRow 
    6             // so the find function returns the row with the ID listed 
    7             DataRow wRow = wTable.Rows.Find(currRow);  
    8             weekOf_lbl.Text = wRow["weekOf"].ToString(); 
    9  
    10             // WebService to fill dataset, _locationID is from a radcombobox item event 
    11             QueryResult qInfo = 
    12                 schedulingClient.SchedulingService.Get_Cal_MA(_locationID, ((DateTime)wRow["startDate"]).Date, ((DateTime)wRow["endDate"]).Date); 
    13             if (!qInfo.Success) 
    14             { 
    15                 Telerik.WinControls.RadMessageBox.Show(qInfo.Message, "Failed to Retrieve Calendar Information"); 
    16                 this.Cursor = Cursors.Default; 
    17                 return
    18             } 
    19             // I use one grid for two sets of values, a radgroupbox toggles the two values 
    20             // with radio buttons inside by modifying the field name only 
    21             if (radio_jobs.ToggleState == Telerik.WinControls.Enumerations.ToggleState.On) 
    22             { 
    23                 this.calendar_grida.Columns["max"].FieldName = "maxjobs"
    24                 this.calendar_grida.Columns["current"].FieldName = "currjobs"
    25                 this.calendar_grida.Columns["available"].FieldName = "availjobs"
    26                 this.calendar_grida.Columns["percentage"].FieldName = "jobPercentage"
    27             } 
    28             else 
    29             { 
    30                 this.calendar_grida.Columns["max"].FieldName = "maxunits"
    31                 this.calendar_grida.Columns["current"].FieldName = "currunits"
    32                 this.calendar_grida.Columns["available"].FieldName = "availunits"
    33                 this.calendar_grida.Columns["percentage"].FieldName = "unitPercentage"
    34             } 
    35             // Ensure when the grouping happens they are in the appropriate order 
    36             qInfo.Ds.Tables[0].DefaultView.Sort = "weekOf, installDate, MA"
    37             this.calendar_grida.DataSource = qInfo.Ds.Tables[0]; 
    38  
    39             // Setup automatic Grouping of WeekOf Date 
    40             this.calendar_grida.MasterGridViewTemplate.GroupByExpressions.Add( 
    41                 "[weekOf] as [Week Of] format \"{0}:  {1:d}\" Group By [weekOf]"); 
    42             // Setup grouping with summary of the values the grid was created for 
    43             string expression =  
    44                 "[dayofWeek] as [Day Of Week] format \"{1}- \", " + 
    45                 "[installDate] as [Insall Date] format \"{1:d}  \", " + 
    46                 "sum(max) as [Max] format \"{0} / {1} \", " + 
    47                 "sum(current) as [Current] format \"{0} / {1} \", " + 
    48                 "sum(available) as [Available] format \"{0} / {1}\" " + 
    49                 "Group By [installDate]"
    50             this.calendar_grida.MasterGridViewTemplate.GroupByExpressions.Add(expression); 
    51             // Expand all groups by default 
    52             this.calendar_grida.MasterGridViewTemplate.ExpandAllGroups(); 
    53             // Color rows by Percentage of capacity (one of my values) 
    54             Telerik.WinControls.UI.ConditionalFormattingObject[] obj = 
    55                 new ConditionalFormattingObject[calendar_grida.Rows.Count]; 
    56             // one for each row for the formatting object 
    57             int counter = 0; 
    58             // one for each groupheader 
    59             int dayCounter = 0; 
    60             // Use this value to only add 1 to the dayCounter when the date changes 
    61             DateTime dt = ((DateTime)wRow["startDate"]).Date; 
    62             // Ensure formatting is cleared before we start 
    63             this.calendar_grida.Columns["id"].ConditionalFormattingObjectList.Clear(); 
    64  
    65             foreach (Telerik.WinControls.UI.GridViewDataRowInfo row in this.calendar_grida.Rows) 
    66             { 
    67                 double percentage = Convert.ToDouble(row.Cells["percentage"].Value); 
    68                 // Start row coloring process by setting the Select object 
    69                 obj[counter] = new Telerik.WinControls.UI.ConditionalFormattingObject("SelectCondition"
    70                 Telerik.WinControls.UI.ConditionTypes.Contains, row.Cells["id"].Value.ToString(), ""true); 
    71                 // Turn all rows green to start with 
    72                 obj[counter].RowBackColor = Color.LightGreen; 
    73                 // Add a day to my day counter (I want to intentionally skip the first one as it's my weekof header 
    74                 if (((DateTime)row.Cells["installDate"].Value).Date > dt) 
    75                 { 
    76                     dayCounter++; 
    77                     dt = dt.AddDays(1); 
    78                 } 
    79                 if ((DateTime)row.Cells["installDate"].Value > DateTime.Now) 
    80                 { 
    81                     // Expand the row IF it's greater than today 
    82                     row.Group.Groups[dayCounter].HeaderRow.IsExpanded = true
    83                     if (percentage >= .6) // Indicate middle tier (yellow) 
    84                     { 
    85                         obj[counter].RowBackColor = Color.Yellow; 
    86                     } 
    87                     if (percentage >= .8) // Indicate upper tier (red) 
    88                     { 
    89                         obj[counter].RowBackColor = Color.Red; 
    90                     } 
    91                     if (percentage > 1.0) // Indicate overbooked (violet) 
    92                     { 
    93                         obj[counter].RowBackColor = Color.Violet; 
    94                     } 
    95                 } 
    96                 else 
    97                 { 
    98                     // Hide Row if it's less than or equal to today 
    99                     row.Group.Groups[dayCounter].HeaderRow.IsExpanded = false
    100                     // Turn all rows Antique White if less than or equal to today 
    101                     obj[counter].RowBackColor = Color.AntiqueWhite; 
    102                 } 
    103                 // Apply the color 
    104                 this.calendar_grida.Columns["id"].ConditionalFormattingObjectList.Add(obj[counter]); 
    105                 counter++; 
    106             } 
    107             // Kick off an event to customize the formatting of the group headers 
    108             this.calendar_grida.GroupSumaryEvaluate += new GroupSummaryEvaluateEventHandler(calendar_grida_GroupSumaryEvaluate); 
    109             this.Cursor = Cursors.Default; 
    110         } 

    And my groupsumaryevaluate event

    1         void calendar_grida_GroupSumaryEvaluate(object sender, GroupSummaryEvaluationEventArgs e) 
    2         { 
    3             // Need a fixed width font for perfect display of values 
    4             e.Group.HeaderRow.GridViewInfo.GridViewElement.Font = 
    5                 new Font("Courier New", e.Group.HeaderRow.GridViewInfo.GridViewElement.Font.Size); 
    6             int i = 0; 
    7             // Days of week maximum width is 9 (Wednesday) so if it's less than 9 add a space 
    8             if (e.SummaryItem.FieldName == "dayofWeek"
    9             { 
    10                 i = 0; 
    11                 while (i <9) 
    12                 { 
    13                     i = e.Value.ToString().Length; 
    14                     e.Value = e.Value + " "
    15                 } 
    16             } 
    17             // Set default length of my summary items for up to 999 to display perfectly 
    18             int length = 3; 
    19             // If I'm using the larger calculation set the length to 5 to allow up to 99999 to display perfectly 
    20             if (radio_units.ToggleState == Telerik.WinControls.Enumerations.ToggleState.On) 
    21             { 
    22                 length = 5; 
    23             } 
    24             // Since the values are all the same general vicinity use one if statement for all 3 
    25             if (e.SummaryItem.FieldName == "max" | 
    26                 e.SummaryItem.FieldName == "available" | 
    27                 e.SummaryItem.FieldName == "current"
    28             { 
    29                 i = e.Value.ToString().Length; 
    30                 while (i < length) 
    31                 { 
    32                     e.Value = " " + e.Value; 
    33                     i = e.Value.ToString().Length; 
    34                 } 
    35             } 
    36         } 

    Thanks,

    Versile

  5. Nick
    Admin
    Nick avatar
    767 posts

    Posted 21 Aug 2009 Link to this post

    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.
Back to Top