2

I'm on Telerik, & i'm trying to format a col values. The column index number 12 is already given a color(it works) and then I want to look for a particular value, if it's present then change the cell color. If the cell value is 4354 then change the cell color....but nothing is happening. What is the mistake in below code???

private int cellValue = 4354;

for (int i = 0; i < TelerikRadGridView.Rows.Count; i++)
 {
 if (TelerikRadGridView.Rows[i].Cells[i].Value == (object)cellValue)
      {
            e.CellElement.BackColor = Color.Gold;
         }
   }

This is the complete code:

private int cellValue = 4354;
private void TelerikRadGridView_CellFormatting(object sender, CellFormattingEventArgs e)
    {

        e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
        e.CellElement.ResetValue(LightVisualElement.NumberOfColorsProperty, ValueResetFlags.Local);
        e.CellElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
        e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);

        if (dontRunHandler == false)
            {
                if (e.CellElement.ColumnIndex != 2 
                && e.CellElement.ColumnIndex != 3 
                && e.CellElement.ColumnIndex != 4
                && e.CellElement.ColumnIndex != 5 
                && e.CellElement.ColumnIndex != 7 
                && e.CellElement.ColumnIndex != 11 
                && e.CellElement.ColumnIndex != 12 
                && e.CellElement.ColumnIndex != 13) return;
                e.CellElement.DrawFill = true;
                e.CellElement.NumberOfColors = 1;
                e.CellElement.BackColor = Color.LightSlateGray;
                e.CellElement.GradientStyle = GradientStyles.Linear;
            }
            for (int i = 0; i < TelerikRadGridView.Rows.Count; i++)
            {
                if (TelerikRadGridView.Rows[i].Cells[i].Value == (object)cellValue)
                {
                    e.CellElement.BackColor = Color.Gold;
                }
            }
KreepN
  • 8,528
  • 1
  • 40
  • 58
linguini
  • 1,939
  • 5
  • 49
  • 79
  • You know the routine by now :). Let me know if my solution works for you. If not, chat time. – KreepN Nov 09 '12 at 04:21

1 Answers1

2

Rewrote it for you so it would do what you want it to:

private int cellValue = 4354;
private void TelerikRadGridView_CellFormatting(object sender, CellFormattingEventArgs e)
{
    e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
    e.CellElement.ResetValue(LightVisualElement.NumberOfColorsProperty, ValueResetFlags.Local);
    e.CellElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
    e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);

    if (dontRunHandler == false)
    {
        if (e.CellElement.ColumnIndex != 2 
        && e.CellElement.ColumnIndex != 3 
        && e.CellElement.ColumnIndex != 4
        && e.CellElement.ColumnIndex != 5 
        && e.CellElement.ColumnIndex != 7 
        && e.CellElement.ColumnIndex != 11 
        && e.CellElement.ColumnIndex != 12 
        && e.CellElement.ColumnIndex != 13) return;
        else
        {
           if(e.CellElement.Value != null)
           {
               if (Double.Parse(e.CellElement.Value.ToString()) != cellValue)
               {
                   e.CellElement.DrawFill = true;
                   e.CellElement.NumberOfColors = 1;
                   e.CellElement.BackColor = Color.LightSlateGray;
                   e.CellElement.GradientStyle = GradientStyles.Linear;
               }
               else
               {
                   e.CellElement.DrawFill = true;
                   e.CellElement.NumberOfColors = 1;
                   e.CellElement.BackColor = Color.Gold;
                   e.CellElement.GradientStyle = GradientStyles.Linear;
               }
           }
        }
    }
KreepN
  • 8,528
  • 1
  • 40
  • 58
  • I changed the BackColor to a ?: expression and avoided the double code with DrawFill etc.. – Vloxxity Nov 09 '12 at 08:07
  • When i use the above, i'm getting a error message `Target Invocation Exception was unhandled`this may be because of bool variable i'm using, the reason in adavnce view I disable all the colored columns. – linguini Nov 09 '12 at 08:36
  • At wich line of code do you got the exception? Are you working with a backgroundworker? If you do so you have to change the UI in the ProgressChange or RunWorkerCompletet events. – Vloxxity Nov 09 '12 at 13:02
  • Vloxxity: I'm not using background worker. I got the exception, at the make the page is visible ex: tespage.visible = true; – linguini Nov 09 '12 at 13:54
  • I'll see u today in the evening about the tech issue i have, thank u :) – linguini Dec 13 '12 at 11:33