Rad:Grid Change Row BackGround Color

13 posts, 0 answers
  1. James
    James avatar
    20 posts
    Member since:
    Oct 2008

    Posted 05 Jun 2009 Link to this post



    Depending on data within row. if column "Behind90Days" is 1 then I need to make the entire row
    within the datagrid some shade of red to alert the user. Have looked through too many examples
    that don't seem to touch of entire row but individual columns?

    In the ItemDataBound event, check the value 'Behind90Days' if it's == 1 then change entire
    background color of that row to red!

    Thanks!

    Jim D

  2. -DJ-
    -DJ- avatar
    263 posts
    Member since:
    Oct 2004

    Posted 06 Jun 2009 Link to this post

    Hi James,

    You can use the databound item and then set the item's backcolor.

    Something like this should work:

    Protected Sub RadGrid1_ItemDataBound(ByVal s As ObjectByVal e As GridItemEventArgs) Handles RadGrid1.ItemDataBound 
        If TypeOf e.Item Is GridDataItem Then 
     
            Dim dataItem As GridDataItem = e.Item 
            Dim myCell As TableCell = dataItem("myCell"
            If myCell.Text = "Behind90Days" Then 
                dataItem.BackColor = Drawing.Color.Red 
                dataItem.ForeColor = Drawing.Color.White 
            End If 
    End If 
    End Sub 
     

    Regards,
    -DJ-

  3. Shinu
    Shinu avatar
    17034 posts
    Member since:
    Mar 2007

    Posted 08 Jun 2009 Link to this post

    Hi James,

    I hope the 'Behind90Days' is the Name of the Column and you are trying to set the entire row color depending on this column value. If so you may try the following approach.

    CS:

     
      protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
        { 
     
            if (e.Item is GridDataItem) 
            { 
             GridDataItem dataItem = e.Item; 
             if (dataItem[Behind90Days].Text == "1"
             { 
                 dataItem.BackColor = Drawing.Color.Red; 
             } 
         } 
     


    Thanks
    Shinu

  4. James
    James avatar
    20 posts
    Member since:
    Oct 2008

    Posted 08 Jun 2009 Link to this post

    With a little of both I figured it out! If anyone see something incorrect here please let me know!
    Bottom line, when databound if it finds 1 (True) in the 90DaysLate column, incorrect before as I
    was using the ID rather than the UniqueName, which caused me some problems! Yech, ID should
    always work I think!

    protected void gridSearchResults_OnItemDataBound(object sender, GridItemEventArgs e)  
        {  
            if (e.Item is GridDataItem)  
            {  
                GridDataItem dataItem = (GridDataItem)e.Item;  
                TableCell myCell = dataItem["90DaysLate"];  
                if ((myCell.Text == "1"))  
                {  
                    //dataItem.BackColor = System.Drawing.Color.Red;  
                    dataItem.ForeColor = System.Drawing.Color.Red;  
                    dataItem.Font.Bold = true;  
                }  
             }
        }
     

  5. -DJ-
    -DJ- avatar
    263 posts
    Member since:
    Oct 2004

    Posted 08 Jun 2009 Link to this post

    Hi James,

    I obviously misread your post, good that Shinu stepped in.
    Glad you got it working.

    Regards,
    -DJ-

  6. Shinu
    Shinu avatar
    17034 posts
    Member since:
    Mar 2007

    Posted 08 Jun 2009 Link to this post

    Hi James,

    You may also go through the following help article which explains how to access the cell values using Column's UniqueName property.
    Accessing cells and rows

    Regards
    Shinu.

  7. Ram
    Ram avatar
    97 posts
    Member since:
    Jun 2011

    Posted 30 Jun 2011 Link to this post

    Hi
    In my radgrid I have set the alternativeitemstyle backcolor=white
    But I'm getting some transparent line after every two rows.
    How can I delete that line.

  8. Shinu
    Shinu avatar
    17034 posts
    Member since:
    Mar 2007

    Posted 30 Jun 2011 Link to this post

    Hello Ram,

    Try the overriding the CSS like below.

    CSS:

    .RadGrid_<skinName>.rgAltRow td
    {
        background-color:#FFFFFF !important;
        border-bottom-color:#FFFFFF !important;
    }

    Thanks,
    Shinu.

  9. Taha
    Taha avatar
    2 posts
    Member since:
    Feb 2012

    Posted 01 Feb 2012 Link to this post

    Hi Every Body!!
    i want to change position tab between cells , so it must change background cell when cell is active...

    tnx

  10. Shinu
    Shinu avatar
    17034 posts
    Member since:
    Mar 2007

    Posted 01 Feb 2012 Link to this post

    Hello Taha,

    I am not quite sure about your requirement. I suppose you want to change the background color of cells. Here is the sample code.
    C#:

    protected void grid_ItemDataBound(object sender, GridItemEventArgs e)
    {
     if (e.Item is GridDataItem)
     {
      GridDataItem item = (GridDataItem)e.Item;
      TableCell cell = (TableCell)item["UniqueName"];
      cell.BackColor = System.Drawing.Color.Red;
     }
    }

    -Shinu.

  11. Paul Zillman
    Paul Zillman avatar
    2 posts
    Member since:
    Jul 2008

    Posted 29 Oct 2012 Link to this post

    I am looking to do the same type of check but on a column setup as a GridCheckBoxColumn.  How would I go about modifying the code to be able to check if the checkbox is checked or not.  Tried to do Dim myCell As CheckBox = dataItem("UniqueName") but get an error "Value of type 'System.Web.UI.WebControls.TableCell' cannot be converted to 'System.Web.UI.WebControls.Checkbox'.  Thanks.

  12. Shinu
    Shinu avatar
    17034 posts
    Member since:
    Mar 2007

    Posted 29 Oct 2012 Link to this post

    Hi,

    I guess you want to access the CheckBoxColumn and assign color for the same cell checking whether it is checked or not.

    C#:

    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            CheckBox chk = (CheckBox)item["UniqueName"].Controls[0];
            if (chk.Checked)
            {
                TableCell cell = (TableCell)item["UniqueName"];
                cell.BackColor = Color.Aqua;
            }
        }
    }

    Thanks,
    Shinu.

  13. Paul Zillman
    Paul Zillman avatar
    2 posts
    Member since:
    Jul 2008

    Posted 30 Oct 2012 Link to this post

    Worked perfect!  Thanks for your help.

Back to Top