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

Change Cell Color Based on Cell Value

5 Answers 812 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Alexander
Top achievements
Rank 1
Alexander asked on 06 Jun 2014, 06:34 PM
I want to be able to change the cell of "Name" to Color.Firebrick if column "Active" is false. I can't seem to be able to change it. I attempted to place the information inside of my sub that takes in GridCommandEventArgs. It looks like this:


Protected Sub myGrid_ShowInactivity(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles myGrid.ItemCommand
            If TypeOf e.Item Is GridDataItem Then
                Dim item As GridDataItem = e.Item
                If item("Active").Text = "false" Then
                    item("Name").Text = item("Name").Text + " INACTIVE"
                    item("Name").BackColor = Color.Firebrick
                    item("Name").ForeColor = Color.White
                End If
            End If
End Sub


For some reason this is not doing anything.

Do I have to put this inside a different sub? If so, what sub would that be?

I also get an error when I attempt any other action, so it seems that this is not the place I should put this code in.

5 Answers, 1 is accepted

Sort by
0
Alexander
Top achievements
Rank 1
answered on 06 Jun 2014, 07:19 PM
I was able to color the cells. But now it colors every cell. Does it matter that the Active column is not visible? Also, I put the item in a different area.
0
Alexander
Top achievements
Rank 1
answered on 06 Jun 2014, 07:37 PM
I am able to color the cells that are truly inactive now. Only one problem remains. The Active column HAS to be visible for this method. I need an easier method.
0
Alexander
Top achievements
Rank 1
answered on 06 Jun 2014, 07:44 PM
After some time, I was able to get it to work correctly. Here is my code in case anyone attempts later on.

Protected Sub myGrid_ColorChange(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles myGrid.ItemDataBound
 
            If TypeOf e.Item Is GridDataItem Then
                Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
                If Not DataBinder.Eval(item.DataItem, "Active") Then
                    Dim cell As TableCell = item("Name")
                    cell.BackColor = Color.Firebrick
                    cell.ForeColor = Color.White
                    cell.Text = cell.Text + " - INACTIVE"
                End If
            End If
 
        End Sub

I used the Not because Active is set to 1 while Inactive is set to 0. So without the Not, it would color all of the Active ones instead of the Inactive ones.
0
Luis
Top achievements
Rank 1
Veteran
answered on 14 Jun 2020, 05:45 PM

In my case I obtain always firebrick background color when Active still value is 1 or 0.

What's wrong? 

Luis

0
Eyup
Telerik team
answered on 17 Jun 2020, 07:08 AM

Hi Luis,

 

You can check the suggestions I've provided here;
https://www.telerik.com/forums/change-the-color-of-text-of-a-radgrid-column#8mfX1A18BkatBdFwEjd7eg

 

Regards,
Eyup
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
Grid
Asked by
Alexander
Top achievements
Rank 1
Answers by
Alexander
Top achievements
Rank 1
Luis
Top achievements
Rank 1
Veteran
Eyup
Telerik team
Share this question
or