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

How To Find the Highest Column Value

4 Answers 410 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Richard Weeks
Top achievements
Rank 2
Richard Weeks asked on 22 Sep 2011, 07:17 AM
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

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 22 Sep 2011, 08:13 AM
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.
0
Richard Weeks
Top achievements
Rank 2
answered on 22 Sep 2011, 11:38 PM

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
0
Richard Weeks
Top achievements
Rank 2
answered on 22 Sep 2011, 11:59 PM
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
0
Sebastian
Telerik team
answered on 27 Sep 2011, 11:34 AM
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
Tags
Grid
Asked by
Richard Weeks
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 2
Richard Weeks
Top achievements
Rank 2
Sebastian
Telerik team
Share this question
or