convert GridviewSummaryItem aggregate value

9 posts, 0 answers
  1. george mcnitt
    george mcnitt avatar
    14 posts
    Member since:
    May 2010

    Posted 29 Sep 2010 Link to this post

    I have the following code and I need to convert the value that comes from GridAggregateFunction.sum,

    Dim summary As New GridViewSummaryItem("dur", " Total: {0} ", GridAggregateFunction.Sum)
               Me.grdResults.MasterTemplate.SummaryRowGroupHeaders.Add(New GridViewSummaryRowItem(New GridViewSummaryItem() {summary}))

    The values in the dur column are in seconds and I need to convert that to hh:mm:ss. I have a function that will do the conversion but don't see how to call it on that value (GridAggregateFunction.Sum) before it is displayed in the grid.

    I tried several things in the ViewCellFormatting and CellFormatting events but that just causes my  app to throw a TargetInvocationException error everytime I start it.

    Any ideas would be greatly apprectiated.
  2. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 30 Sep 2010 Link to this post

    Hello George,

    One example would be:
    First create the summary row and the summary item:

    GridViewSummaryItem summaryItem = new GridViewSummaryItem("Id", GetResultString("{0}"), GridAggregateFunction.Sum);
    var summaryRow = new GridViewSummaryRowItem();
    summaryRow.Add(summaryItem);
    this.radGridView1.SummaryRowsTop.Add(summaryRow);
    this.radGridView1.MasterView.SummaryRows[0].PinPosition = PinnedRowPosition.Top;

    In the format string you can add any method with a string result, for example:
    private string GetResultString(string p)
    {
        var result = p;
        // do all the necessary calculations here
        return result;
    }

    It should now work as expected.

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
  3. UI for WinForms is Visual Studio 2017 Ready
  4. george mcnitt
    george mcnitt avatar
    14 posts
    Member since:
    May 2010

    Posted 30 Sep 2010 Link to this post

    All that does is literally pass {0} to my function.

    Here is the line defined as you stated.

     

     

     

     

    Dim summary2 As New GridViewSummaryItem("dur", formatTime("{0}"), GridAggregateFunction.Sum)

     

    Here is the formatTime function.

    Function formatTime(ByVal i As String) As String
           Dim j As Int32 = Convert.ToInt32(i)
           Dim strRet As String = String.Empty
           Dim tHr As Int32
           If (j <> 0) Then
               If Fix(j / 3600) = 0 Then
                   strRet = formatDigits(Fix(j / 60)) & ":" & formatDigits(j Mod 60)
               Else
                   tHr = formatDigits(Fix(j / 3600))
                   strRet = tHr & ":" & formatDigits(Fix((j - tHr * 3600) / 60)) & ":" & formatDigits(j Mod 60)
               End If
           End If
           formatTime = strRet
       End Function

    When formatTime receives the value it is {0} and not the string like "1234".
  5. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 30 Sep 2010 Link to this post

    Hello again,

    Try it in c# and it will work, honestly i don't know the string.Format for vb.

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
  6. george mcnitt
    george mcnitt avatar
    14 posts
    Member since:
    May 2010

    Posted 30 Sep 2010 Link to this post

    I am not developing in C#, The whole app is VB. This is a large project its not something I can go back and rewrite in C#.

    Although I can't see why it should be any different in C#. they both have the same signature and same parameters so I should be able to do the same thing you are saying can be done using C#.

    Thanks for the help.
  7. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 30 Sep 2010 Link to this post

    Hello George,

    I have created a vb version of the form, very simple one:
    VB:
    Imports System
    Imports System.ComponentModel
    Imports System.Windows.Forms
    Imports Telerik.WinControls.UI
     
    Public Class Form1
     
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim RadGridView1 = New RadGridView()
            RadGridView1.Dock = DockStyle.Fill
            RadGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill
            Me.Controls.Add(RadGridView1)
            RadGridView1.DataSource = New TestsCollection(10)
            Dim summaryItem As New GridViewSummaryItem("Id", GetResultString("{0}"), GridAggregateFunction.Sum)
            Dim summaryRow = New GridViewSummaryRowItem()
            summaryRow.Add(summaryItem)
            RadGridView1.SummaryRowsTop.Add(summaryRow)
            RadGridView1.MasterView.SummaryRows(0).PinPosition = PinnedRowPosition.Top
        End Sub
     
        Private Function GetResultString(ByVal p As String) As String
            Return "Result: " + p
        End Function
    End Class
     
    Public Class Test
        Public Property Id() As Integer
            Get
                Return m_Id
            End Get
            Set(ByVal value As Integer)
                m_Id = Value
            End Set
        End Property
        Private m_Id As Integer
     
        Public Sub New(ByVal id As Integer)
            Me.Id = id
        End Sub
    End Class
     
    Public Class TestsCollection
        Inherits BindingList(Of Test)
        Public Sub New(ByVal noItems As Integer)
            For i As Integer = 1 To noItems
                Me.Add(New Test(i))
            Next
        End Sub
    End Class

    and C#:
    using System;
    using System.ComponentModel;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;
     
    namespace TestSummaryRows
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                this.Load += new EventHandler(Form1_Load);
            }
     
            void Form1_Load(object sender, EventArgs e)
            {
                var radGridView1 = new RadGridView();
                radGridView1.Dock = DockStyle.Fill;
                radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
                this.Controls.Add(radGridView1);
                radGridView1.DataSource = new TestsCollection(10);
                GridViewSummaryItem summaryItem = new GridViewSummaryItem("Id", GetResultString("{0}"), GridAggregateFunction.Sum);
                var summaryRow = new GridViewSummaryRowItem();
                summaryRow.Add(summaryItem);
                radGridView1.SummaryRowsTop.Add(summaryRow);
                radGridView1.MasterView.SummaryRows[0].PinPosition = PinnedRowPosition.Top;
            }
     
            private string GetResultString(string p)
            {
                return "Result: " + p;
            }
        }
     
        public class Test
        {
            public int Id { get; set; }
     
            public Test(int id)
            {
                this.Id = id;
            }
        }
     
        public class TestsCollection : BindingList<Test>
        {
            public TestsCollection(int noItems)
            {
                for (int i = 1; i <= noItems; i++)
                {
                    this.Add(new Test(i));
                }
            }
        }
    }

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
  8. george mcnitt
    george mcnitt avatar
    14 posts
    Member since:
    May 2010

    Posted 30 Sep 2010 Link to this post

    If you actually step through your example code you will see that it is not doing anything to the actuall value of GridAggregateFuntion.Sum in your GetResultString.

    All its doing is passing the literal "{0}" to the function and appending it to "Result: ".

    To see exactly what I mean trying doing an actual calulation on the value "p"  like p * 10  
     in your GetResultString function and you will see that your return value is actaully nothing more than "Result: {0}", which is the format string that GridViewSummaryItem  is expecting.

    you would get the exact same result as if you had just done:

    Dim summaryItem As New GridViewSummaryItem("Id", "Result: {0}"), GridAggregateFunction.Sum)


    No actuall value is being sent, just the string literal "{0}"

    That is the problem I am having, I want to actually run a function against the VALUE that is returned and not build a format string.

    Here is an example. Change your GetResultString function to this.
    Private Function GetResultString(ByVal p As String) As String
            Return "Result: " & (Convert.ToInt32(p) * 10).ToString
        End Function

    And you will see that when Convert trys to change p to an int from string "{0}" which is what it really recieved you will get an "Input string was not in a correct format." error.
  9. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 01 Oct 2010 Link to this post

    Hello again,

    First i want to appologize because i didn't test it thorough enough, i was assuming that the string format arguments were calculated before sending, i don't know why...

    Second, from what i've been trying here, there is no way of doing that directly, i have found a workaround which you can use, but i have to warn you, it's pretty strange, but it's working.

    So, first, we will have to access the evaluation mechanism directly, so you should change the summary item declaration to this:
    Dim summaryItem As New GridViewSummaryItem(
               "Id",
               formatTime(radGridView1.MasterTemplate.DataView.Evaluate(
                                                       "Sum(Id)",
                                                       0,
                                                       radGridView1.RowCount)),
               GridAggregateFunction.Sum)

    and with this you will have a real result in the formatTime function (object this time, not string), just like:
    Function formatTime(ByVal i As Object) As String
        Dim number = Convert.ToInt32(i) * 30 + 5
        ' do all the calculations here
        Return number.ToString()
    End Function

    Now, the problems are as follows:
    1. You will have to have the grid data bound, or just data in the grid before creating the summary row, second, because you are always giving a calculated value to the summary row, the value should always be recalculated on CellValueChanged, just like:
    Private Sub radGridView1_CellValueChanged(ByVal sender As Object, ByVal e As GridViewCellEventArgs) Handles radGridView1.CellValueChanged
        If e.Column.Name = "Id" Then
            radGridView1.MasterView.SummaryRows(0).SummaryRowItem(0).FormatString = formatTime(radGridView1.MasterTemplate.DataView.Evaluate("Sum(Id)", 0, radGridView1.RowCount))
            radGridView1.Refresh()
        End If
    End Sub

    Like this it is working, but i have to say, it's a pretty ugly workaround... i honestly hope they will add a ValueChanged event with a value, or at least a CustomFormatter option.

    Hope this helps,

    Best Regards,
    Emanuel Varga
  10. Alexander
    Admin
    Alexander avatar
    306 posts

    Posted 01 Oct 2010 Link to this post

    Hello guys,

    When each SummaryItem is evaluated, the GroupSummaryEvaluate event is fired. Its EventArgs contain the evaluated Value and the FormatString, which you can modify. Please review this help article containing examples for GroupSummaryEvaluate event usage. It works the same way for group header row and summary items.

    Best regards,
    Alexander
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top
UI for WinForms is Visual Studio 2017 Ready