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

[Solved] Format Cell based on value

6 Answers 201 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chrysa
Top achievements
Rank 1
Chrysa asked on 04 May 2009, 02:30 PM
Hi folks:
Can someone tell me how to programmatically format a grid cell based on value?

Example:  anything less than/equal 1 shows as percent and anything more than 1 shows as number.

Thanks in advance!

6 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 05 May 2009, 03:49 AM
Hi Chrysa,

Check out the following help article which explains how to achieve conditional formatting of Grid cells.
http://www.telerik.com/help/aspnet-ajax/grdconditionalformatting.html

Shinu
0
Chrysa
Top achievements
Rank 1
answered on 05 May 2009, 10:43 AM
Thanks Shinu - much appreciated
0
Chrysa
Top achievements
Rank 1
answered on 05 May 2009, 10:51 AM
ok - this answer is what I needed (almost) -- now how would I apply this logic to a pivoted gridview where the names of the columns change every day...??
0
Princy
Top achievements
Rank 2
answered on 05 May 2009, 01:04 PM
Hello,

Check out the following code to format your cells based on the cell values:
c#:
protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
       foreach (GridColumn col in RadGrid1.MasterTableView.RenderColumns) 
        { 
            foreach (GridDataItem dataItem in RadGrid1.Items) 
            { 
                if ((dataItem)[col.UniqueName].Text == "Text1"
                { 
                    (dataItem)[col.UniqueName].BackColor = System.Drawing.Color.Blue; 
 
                } 
            } 
        } 
    } 

You can alter the logic according to your requirement.

Thanks
Princy.
0
Chrysa
Top achievements
Rank 1
answered on 05 May 2009, 02:24 PM
Hi Princy/Shinu:
OK - after some trial and error the page displays but does nothing...
here's what I've done...

 

protected void RadGrid2_PreRender(object sender, EventArgs e)

 

{

 

 

    foreach (GridColumn col in RadGrid2.MasterTableView.RenderColumns)

 

        {

 

 

            foreach (GridDataItem dataItem in RadGrid2.Items)

 

                {

 

 

                    if (((dataItem)[col.UniqueName].Text == null))

 

                        {

                        }

 

 

                    else if ((int.Parse((dataItem)[col.UniqueName].Text) > 0) && (int.Parse((dataItem)[col.UniqueName].Text) <= 1))

                    //else if ((decimal.Parse((dataItem)[col.UniqueName].Text) > 0) && (decimal.Parse((dataItem)[col.UniqueName].Text) <= 1))

 

 

 

 

                    {

                        (dataItem)[col.UniqueName].Text =

 

 

string.Format( "{0:00%}");

 

                    }

 

 

                    else

 

 

                    {

                    }

                }

         }

}

the pivot displays but the numbers don't format.  What am i doing wrong?

thanks

0
Shinu
Top achievements
Rank 2
answered on 06 May 2009, 04:52 AM
Hi Chrysa,

Can you apply formatting for the Grid cell as shown below and see whether it is working.

CS:
 
(dataItem)["Test"].Text = string.Format("{0:00%}"int.Parse(dataItem[col.UniqueName].Text));  


Shinu.



Tags
Grid
Asked by
Chrysa
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Chrysa
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or