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

Formatting of row based on cell value

6 Answers 284 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Felice
Top achievements
Rank 1
Felice asked on 20 Jun 2014, 11:49 AM
I need to change the formatting of some of the field in the row/record if one of them contains any value.
I tried this:

 if (e.Item is GridDataItem)
        {
            GridDataItem dataBoundItem = e.Item as GridDataItem;
            if ((dataBoundItem["AlternCost"].Text) != null)
            {
                dataBoundItem["AlternCost"].ForeColor = Color.Red;
                dataBoundItem["AlternCost"].Font.Bold = true;
                dataBoundItem["AlternCost"].BackColor = Color.Yellow;
                dataBoundItem["TotCost"].ForeColor = Color.Red;
                dataBoundItem["TotCost"].Font.Bold = true;
            }
        }

but I am experiencing problems because the back color applies to the entire column if the condition is true and I need it to be applied only to the "AlternCost" cell/row of the record where the condition occurs.
Please help!

Thanks

6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 20 Jun 2014, 11:59 AM
Hi Felice,

Your code works fine at my end. Please try the following approach and check if it works.

C#:
if (e.Item is GridDataItem)
{
  GridDataItem dataBoundItem = e.Item as GridDataItem;
  TableCell cell = dataBoundItem["AlternCost"];
  if ((cell.Text)!= null)
  {        
      cell.ForeColor = Color.Red;
      cell.Font.Bold = true;
      cell.BackColor = Color.Yellow;          
  }
}

Thanks,
Princy
0
Felice
Top achievements
Rank 1
answered on 20 Jun 2014, 03:16 PM
Hi Princy,
first of all thanks a lot for supporting.
With the code you suggested I have exactly the same problem I have with mine.
Basically the entire column get the background Yellow. 
See the picture attached.
It should not be like that. I expect only the specific cell to get the formatting!
I do not really know what to do with this problem. Could fit be a bug?
0
Felice
Top achievements
Rank 1
answered on 20 Jun 2014, 04:03 PM
Hi Princy,
for you info, I even try to "reset" the cells where the condition does not apply:

if (e.Item is GridDataItem)
       {
           GridDataItem dataBoundItem = e.Item as GridDataItem;
           TableCell cell = dataBoundItem["AlternCost"];
           
           if ((cell.Text) != null)
           {
               cell.ForeColor = Color.Red;
               cell.Font.Bold = true;
               cell.BackColor = Color.Yellow;
           }
           else
           {
               cell.ForeColor = Color.Black;
               cell.Font.Bold = false;
               cell.BackColor = Color.White;
           }
But it does not produce any effect. Th problem of the background color yellow to the entire column is not resolved. 
0
Princy
Top achievements
Rank 2
answered on 21 Jun 2014, 03:06 AM
Hi Felice,

I guess you donot want to highlight empty rows. The issue is that the cell values are not null but " ", hence you have to check condition for this as shown below.

C#:
if ((cell.Text) != "$nbsp;") // Checks if cell is not empty or doesn't contain any non-breaking space

Thanks,
Princy
0
Felice
Top achievements
Rank 1
answered on 21 Jun 2014, 06:26 AM
Hi Princy,
thanks for taking the time to support me.
I tried as you suggested if ((cell.Text) != "$nbsp;") 
but still the problem persist. I Also tried as following:
 protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
 if (e.Item is GridDataItem)
        {
            GridDataItem dataBoundItem = e.Item as GridDataItem;
            TableCell cell = dataBoundItem["AlternCost"];


           // if ((cell.Text) != "$nbsp;") 
                if (string.IsNullOrEmpty(cell.Text))
            {   
                cell.ForeColor = Color.Black;
                cell.Font.Bold = false;
                cell.BackColor = Color.White;
            }
            else
            {
                cell.ForeColor = Color.Red;
                cell.Font.Bold = true;
                cell.BackColor = Color.Yellow;
            }
        }
}
but again the problem persist. The entire column has yellow back color.
But even worst!!! I just discovered that even if there are no values in any of the column cell, the entire column get the back color applied! And this is something I really do not understand. I am wondering if this could be a bug.


0
Felice
Top achievements
Rank 1
answered on 22 Jun 2014, 05:26 AM
Solved!

Hi Princy, thanks for putting me on the right direction.
Finally the problem was solved as following:

GridDataItem dataBoundItem = e.Item as GridDataItem;
           TableCell cell = dataBoundItem["AlternCost"];
 
           #region Format SpecialCost in red if it exist in row
           //Refer http://www.telerik.com/forums/cell-with-a-null-value-returns-160 to understand why there is  
           //If Cell's value is neither " " nor empty string, than format it
           if ((cell.Text.Trim()) != " " && !String.IsNullOrWhiteSpace(cell.Text.Trim()))
           {
               cell.ForeColor = Color.Red;
               cell.Font.Bold = true;
               cell.BackColor = Color.Yellow;
           }
           #endregion

Thank you,
Felice
Tags
Grid
Asked by
Felice
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Felice
Top achievements
Rank 1
Share this question
or