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

Color Individual cells

9 Answers 197 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Doug Odegaard
Top achievements
Rank 2
Doug Odegaard asked on 09 Dec 2008, 10:06 PM
Does anyone have a way to make cells in a row have a unique color based on having a particular value?  It is "Conditional Formatting" only for a single cell versus an entire row.  Each row could have multiple occuranges of this value and I'd like to change each of these cells to yellow.  Ideas?  Not an entire column or an entire row......just individual cells.

Thanks in advance.
Doug

9 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 10 Dec 2008, 03:58 AM
Hello Doug,

You can try out the following code to apply styles to individual cells based on a particular value across the grid.
cs:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
  { 
       if (e.Item is GridDataItem) 
            { 
                GridDataItem dataItem = (GridDataItem)e.Item; 
                foreach (GridColumn col in RadGrid1.Columns) 
                { 
                   if(dataItem[col.UniqueName].Text == "Text") 
                        dataItem[col.UniqueName].BackColor = System.Drawing.Color.Yellow; 
                }                
            } 
  } 

Thanks
Princy.
0
Doug Odegaard
Top achievements
Rank 2
answered on 10 Dec 2008, 05:30 PM
Princy,
You rock!  Thanks so much for the reply.  The only issue I have is that the data coming into the RadGrid is using AutoGenerated columns which ends up as null objects using the code below.  I think I have harvested autogenerated columns before and done things with them but do you or anyone else have an easy way to manipulate autogenerated columns.  The reason I use this is so that the Header Rows actually contain dates.  These dates (eg Mon-29 for Monday, Dec 29th for instance) change as each datatable is bound to the grid. 

Thanks in advance.
Doug
0
Doug Odegaard
Top achievements
Rank 2
answered on 10 Dec 2008, 11:31 PM
I figured it out....here is a modified version of the code for anyone else figuring out how to do it.  Thanks princy.

    protected void radSchedule_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridDataItem) 
        { 
            GridDataItem dataItem = (GridDataItem)e.Item; 
 
            foreach (GridColumn col in ((Telerik.Web.UI.GridItem)(dataItem)).OwnerTableView.AutoGeneratedColumns) 
            { 
                if (dataItem[col.UniqueName].Text == "JRK"
                    dataItem[col.UniqueName].BackColor = System.Drawing.Color.Yellow; 
            } 
 
             
        } 
    } 

0
Shinu
Top achievements
Rank 2
answered on 11 Dec 2008, 03:47 AM
Hi,

You can also refer the following help article to get more details on this regard.
Conditional Formatting for rows/cells on ItemDataBound

Shinu
0
Eric
Top achievements
Rank 1
answered on 30 Mar 2009, 01:10 PM
Does anyone have this coded in VB?
0
Princy
Top achievements
Rank 2
answered on 30 Mar 2009, 01:43 PM
Hello Eric,

Here is the code in VB for the conditional formatting for rows/cells on ItemDataBound :
VB:
Protected Sub RadGrid1_ItemDataBound(ByVal sender As ObjectByVal e As  Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound 
         If (TypeOf e.Item Is GridDataItem) Then 
             Dim dataItem As GridDataItem = CType(e.Item, GridDataItem) 
             If (dataItem("Country").Text = "Mexico"Then 
                dataItem.CssClass = "MyMexicoRowClass" 
             End If 
         End If 
End Sub 

css:
<style type="text/css"
 .MyMexicoRowClass 
 { 
   background-coloraqua
   font-size16px
   fond-family: Arial
 } 
</style> 

Also you can use the following code conversion tool to convert C# code to VB code:
Conversion tool

Thanks
Princy.
0
Eric
Top achievements
Rank 1
answered on 30 Mar 2009, 01:51 PM
Thanks I was close but had a minor error.

0
Brian Azzi
Top achievements
Rank 2
answered on 12 Apr 2010, 04:49 PM
I realize this is a bit of an old topic, but its very much along the lines of something I'm trying to do...

My question is this (regarding the above post): that's all well & good to change the background color conditionally, but in doing so it completely breaks all of the other styles associated with that row (selected background, grid lines, etc, etc).

Does anyone know of a way to preserve this, while still changing background color? All I want to do is basically create 3 or 4 different background colors based on a condition, but preserve the other styling of the grid.

Thanks,
-Brian
0
Brian Azzi
Top achievements
Rank 2
answered on 12 Apr 2010, 08:52 PM
Just a tip in case anyone else wanders into this... I found that I could force the selected row style by overriding the default style and adding an !important tag... this way when the row is selected, the selected row image still is used (as opposed to any custom background color and/or background image). I also had to go through each cell on the server side and manually adjust padding to make sure that the row matched any normal rows. I'm sure this could also be done via CSS.

Just thought I'd share. ; )
-Brian
Tags
Grid
Asked by
Doug Odegaard
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 2
Doug Odegaard
Top achievements
Rank 2
Shinu
Top achievements
Rank 2
Eric
Top achievements
Rank 1
Brian Azzi
Top achievements
Rank 2
Share this question
or