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

CheckBoxColumn Colored CheckMark Primitive?

3 Answers 111 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ian
Top achievements
Rank 1
Ian asked on 10 Oct 2011, 02:46 PM
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;
    }
}


3 Answers, 1 is accepted

Sort by
0
Ian
Top achievements
Rank 1
answered on 10 Oct 2011, 03:06 PM
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);
    }
}
0
Accepted
Ivan Petrov
Telerik team
answered on 13 Oct 2011, 08:45 AM
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.

0
Ian
Top achievements
Rank 1
answered on 13 Oct 2011, 12:12 PM
Brialliant, Ivan.

Thanks
Ian
Tags
GridView
Asked by
Ian
Top achievements
Rank 1
Answers by
Ian
Top achievements
Rank 1
Ivan Petrov
Telerik team
Share this question
or