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

Conditional formatting for footer

6 Answers 308 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Syed
Top achievements
Rank 1
Syed asked on 17 May 2009, 01:03 PM
Hi,

I can able to format the cells of the RadGrid.  I want to format footer and group footer based on some condition like in the following code. 
Look at the following code which i format for the cell:
Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound  
        'Is it a GridDataItem  
        If (TypeOf (e.Item) Is GridDataItem) Then  
            'Get the instance of the right type  
            Dim dataBoundItem As GridDataItem = e.Item  
 
            Dim a As New ArrayList  
            a.Add("M9_Sep")  
            a.Add("M10_Oct")  
            a.Add("M11_Nov")  
            a.Add("M12_Dec")  
            a.Add("M1_Jan")  
            a.Add("M2_Feb")  
            a.Add("M3_Mar")  
            a.Add("M4_Apr")  
            a.Add("M5_May")  
 
            For i As Integer = 0 To 8  
                Dim s As Integer = 0 
                Integer.TryParse(dataBoundItem(a(i)).Text, s)  
 
                Select Case s  
                    Case Is > 120  
                        dataBoundItem(a(i)).BackColor = Drawing.Color.Red  
                    Case 90 To 120  
                        dataBoundItem(a(i)).BackColor = Drawing.Color.Green  
                    Case 60 To 89  
                        dataBoundItem(a(i)).BackColor = Drawing.Color.Yellow  
                    Case Is < 60 
                        dataBoundItem(a(i)).BackColor = Drawing.Color.Purple  
                End Select  
            Next  
        End If 

Thanks.

Regards
Syed Arshad

6 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 18 May 2009, 05:25 AM
Hi Syed,

You need to access the footer as GridFooterItem and group footer as GridGroupFooterItem in the ItemDataBound event.

CS:
 
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridFooterItem) 
        { 
            GridFooterItem pager = (GridFooterItem)e.Item; 
            //apply formatting here 
        } 
        if (e.Item is GridGroupFooterItem) 
        { 
            GridGroupFooterItem footer = (GridGroupFooterItem)e.Item; 
            //apply formatting here 
        } 
   } 


Princy.
0
Daniel Aquere
Top achievements
Rank 2
answered on 14 Apr 2011, 04:42 AM
Hi Princy and Syed,

Please, how can I test the value of footer column (sum) in C# to decide what formatting (colors) I use?

Thanks, best

Daniel
0
Princy
Top achievements
Rank 2
answered on 18 Apr 2011, 07:22 AM
Hello Daniel,

You can access the footer column value using UniqueName of the corresponding column.

C#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
   {
       if (e.Item is GridFooterItem)
       {
           GridFooterItem footerItem = (GridFooterItem)e.Item;
           if (footerItem["ColumnUniqueName"].Text == "xxx")
           {
             //your code
           }
       }
   }

Thanks,
Princy.
0
Daniel Aquere
Top achievements
Rank 2
answered on 03 May 2011, 03:02 AM
Hi Princy,

Works very well.

Thanks, best

Daniel
0
Daniel Aquere
Top achievements
Rank 2
answered on 02 Aug 2011, 03:44 AM
Hi Princy,

Please, one more doubt...

In my grid I have four columns:
1. activity duration
2. percent_finished of activity (0 to 100)
3. project duration
4. activity duration percent (regarding to the project duration) - if the project duration is 100 day and the activity duration is 10 days, this column value is 10%

I´d like to agregate on footer with "Sum" only the values items with column 2 greater then 0%, but, it´s necessary something like "activity duration" * "percent_finished of activity" to discover the real percent finished.

Translating, I have a list of activities of project and need to discover the percent finished.

How to do this in the footer?

Thanks,

Daniel
0
Tsvetina
Telerik team
answered on 04 Aug 2011, 03:50 PM
Hi Daniel Aquere,

I have answered your question in the support thread that you have opened. If you have any further questions on that matter, please post them there to avoid duplicate posts. Here is the answer from the ticket for anyone who could need it:
You can achieve this through the ItemDataBound event of RadGrid. Since it is first fired for each grid dataitem and then for the footer, you can first sum up the values for each row and then put them in the footer. I did not exactly understand the logic of the calculation but I did a sample one in your project, so that you can see how the implementation works. Then, you can modify it as per your requirement.
int total;
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem dataItem = e.Item as GridDataItem;
        int fieldValue = int.Parse(dataItem["duracao"].Text) * int.Parse((dataItem["percent_concluido"].FindControl("percent_concluido") as Label).Text);
        total += fieldValue;
    }
    if (e.Item is GridFooterItem)
    {
        GridFooterItem footerItem = e.Item as GridFooterItem;
        footerItem["percent_concluido"].Text = "total: " + total.ToString();
    }
}


This is based on the code sample from this documentation article:
Totals in Grid Footers

Greetings,
Tsvetina
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

Tags
Grid
Asked by
Syed
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Daniel Aquere
Top achievements
Rank 2
Tsvetina
Telerik team
Share this question
or