CheckBoxColumn Colored CheckMark Primitive?

4 posts, 1 answers
  1. Ian
    Ian avatar
    61 posts
    Member since:
    Jul 2010

    Posted 10 Oct 2011 Link to this post

    Hi Telerik

    I'm trying to color the checkmarks in two GridViewCheckBoxColumns in a grid in our app. I have tried the following code but have only succeeded in producing visual gibberish. The entire grid is destroyed - my fault  :-)

    Could you please provide some guidance.
    Many thanks in advance
    regards
    Ian Carson

    void rgvOperationsGrid_ViewCellFormatting(object sender, CellFormattingEventArgs e)
    {
        if (e.ColumnIndex == 2)
        {
            ((RadCheckBoxElement)e.CellElement.Children[0]).CheckMarkPrimitive.CheckElement.ForeColor = Color.DarkGreen;
        }
        else if (e.ColumnIndex == 3)
        {
            ((RadCheckBoxElement)e.CellElement.Children[0]).CheckMarkPrimitive.CheckElement.ForeColor = Color.DarkRed;
        }
    }


  2. Ian
    Ian avatar
    61 posts
    Member since:
    Jul 2010

    Posted 10 Oct 2011 Link to this post

    Hi Again

    As usual I keep worrying away at these problems and have come up with a solution which gets me 95% there. I changed to the cellformatting event, used the Rad Control Spy to get the structure and came up with the code below

    Can you provide guidance as to whether my reset is correct or indeed even needed.

    Regards
    Ian

    void rgvOperationsGrid_CellFormatting(object sender, CellFormattingEventArgs e)
    {
        if (e.ColumnIndex == 2)
        {
            e.CellElement.ForeColor = Color.DarkGreen;
            ((RadCheckBoxEditorElement)e.CellElement.Children[0]).Checkmark.CheckElement.ForeColor = Color.DarkGreen;
        }
        else if (e.ColumnIndex == 3)
        {
            e.CellElement.ForeColor = Color.DarkRed;
            ((RadCheckBoxEditorElement)e.CellElement.Children[0]).Checkmark.CheckElement.ForeColor = Color.DarkRed;
        }
        else
        {
            e.CellElement.ResetValue(LightVisualElement.ForeColorProperty);
        }
    }
  3. Answer
    Ivan Petrov
    Admin
    Ivan Petrov avatar
    705 posts

    Posted 13 Oct 2011 Link to this post

    Hi Ian,

    Thank you for writing and for the follow up message.

    You have found the right approach in the current situation and your code almost does the job and only misses one little addition. You have to reset the value you set in the if statements. This is so, because if you have other check box columns, they might get the fore color of these two columns when you scroll, because the grid reuses its visual elements through virtualization. I have modified your code a bit and now it should work as you would expect it to.

    private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
    {
      if (e.ColumnIndex == 2)
      {
        CheckPrimitive check = ((RadCheckBoxEditorElement)e.CellElement.Children[0]).Checkmark.CheckElement;
        check.SetValue(CheckPrimitive.ForeColorProperty, Color.DarkGreen);
      }
      else if (e.ColumnIndex == 3)
      {
        CheckPrimitive check = ((RadCheckBoxEditorElement)e.CellElement.Children[0]).Checkmark.CheckElement;
        check.SetValue(CheckPrimitive.ForeColorProperty, Color.DarkRed);
      }
      else
      {
        if (e.Column is GridViewCheckBoxColumn)
        {
          CheckPrimitive check = ((RadCheckBoxEditorElement)e.CellElement.Children[0]).Checkmark.CheckElement;
          check.ResetValue(CheckPrimitive.ForeColorProperty, ValueResetFlags.Local);
        }
      }
    }

    You will need to add usings for the Telerik.WinControls.Primitives and Telerik.WinControls namespaces for this code to compile.

    I hope this will help you. If you need further assistance, I would be glad to help. Regards,
    Ivan Petrov
    the Telerik team

    Q2’11 SP1 of RadControls for WinForms is available for download (see what's new); also available is the Q3'11 Roadmap for Telerik Windows Forms controls.

  4. Ian
    Ian avatar
    61 posts
    Member since:
    Jul 2010

    Posted 13 Oct 2011 Link to this post

    Brialliant, Ivan.

    Thanks
    Ian
Back to Top