Change Cell Color Based on Cell Value

6 posts, 0 answers
  1. Alexander
    Alexander avatar
    67 posts
    Member since:
    Jun 2014

    Posted 06 Jun 2014 Link to this post

    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.
  2. Alexander
    Alexander avatar
    67 posts
    Member since:
    Jun 2014

    Posted 06 Jun 2014 in reply to Alexander Link to this post

    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.
  3. Alexander
    Alexander avatar
    67 posts
    Member since:
    Jun 2014

    Posted 06 Jun 2014 Link to this post

    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.
  4. Alexander
    Alexander avatar
    67 posts
    Member since:
    Jun 2014

    Posted 06 Jun 2014 Link to this post

    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.
  5. Luis
    Luis avatar
    11 posts
    Member since:
    Nov 2009

    Posted 14 Jun 2020 in reply to Alexander Link to this post

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

    What's wrong? 

    Luis

  6. Eyup
    Admin
    Eyup avatar
    4046 posts

    Posted 17 Jun 2020 Link to this post

    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.
Back to Top