How To Find the Highest Column Value

5 posts, 0 answers
  1. Richard Weeks
    Richard Weeks avatar
    95 posts
    Member since:
    Oct 2009

    Posted 22 Sep 2011 Link to this post

    How can I programmatically identify and retrieve the cell value in a column with the highest value for that colum?
    So in a grid like:

    MyValues
    --------
    1
    2
    3
    4

    I would get "4".

    This would happen in the OnItemDataBound event with e.Item being GridFooterItem.

    Thanks,
    Richard
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 22 Sep 2011 Link to this post

    Hello Richard,

    You can achieve this by using the footer. Hook the ColumnCreated event and set the Aggregate function as Max:
    C#:
    protected void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
       {
           if ( e.Column.UniqueName=="EmployeeID")
           {
               GridBoundColumn col = (GridBoundColumn)e.Column;
               col.Aggregate = GridAggregateFunction.Max;
           }
           
       }

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Richard Weeks
    Richard Weeks avatar
    95 posts
    Member since:
    Oct 2009

    Posted 22 Sep 2011 Link to this post

    Hi, thanks for your reply.

    In your example, which I tried, e.Column.UniqueName is always equal to "ExpandColumn".

    What I'm trying to do is in the OnItemDataBound event, run a calculation and display the result in the footer:

    private decimal totalOne;

    private decimal totalTwo;

    protected
    void Grid_OnItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            var gridItem = e.Item as GridDataItem;
      
            switch (gridItem.ItemType)
            {
                case GridItemType.AlternatingItem:
                case GridItemType.Item:
      
                    totalOne += Conversion.ConvertToDecimal(gridItem["One"].Text);
                    totalTwo += Conversion.ConvertToDecimal(gridItem["Two"].Text);
      
                    break;
            }
        }
      
        if (e.Item is GridFooterItem)
        {
            var footerItem = e.Item as GridFooterItem;
      
            if (this.totalOne != 0 && this.totalTwo != 0)
            {
                decimal calculatedValue = (this.totalTwo * 100) / this.totalOne;
      
                string displayValue = calculatedValue.ToString("N1");
      
                footerItem["One"].Text = displayValue;
            }
        }
    }

    Apologies if the code is not quite right, I just quickly threw it together. I don't yet know if I need to track the totals better than this.

    The problem I have is that "One" / "totalOne" must be the maximum value held in that column.

    Help much appreciated!

    Richard
  5. Richard Weeks
    Richard Weeks avatar
    95 posts
    Member since:
    Oct 2009

    Posted 22 Sep 2011 Link to this post

    Isn't it amazing what can happen when you drag a colleague over, subject them to a finger stabbing tirade and then some madman swicthes a light on in your head? Or it might just be me :)

    var tempValue = Conversion.ConvertToDecimal(gridItem["One"].Text);
      
    if (tempValue != null)
    {
        if (tempValue > totalOne)
        {
            totalOne = tempValue;
        }
    }

    What I'd like to know is whther the above makes sense as an approach or whether the scientist is indeed mad.

    Richard
  6. Sebastian
    Admin
    Sebastian avatar
    9934 posts

    Posted 27 Sep 2011 Link to this post

    Hello Richard,

    The solution proposed by Shinu will work in case you have auto-generated columns in your grid, and will not be applicable with declarative columns.

    When having columns defined in the markup, you may consider the approach presented in this documentation topic. Alternatively, you may intercept the PreRender event of the grid and set the aggregates for the columns that will be calculated automatically there, as explained here (see the bottom section).

    Your implementation also seems to be doable.

    Best regards,
    Sebastian
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017