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

GridViewComboBoxColumn Background Color

4 Answers 131 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Kenneth
Top achievements
Rank 1
Kenneth asked on 24 Jun 2011, 10:18 PM
Is it possible to format the selections in a GridViewComboBoxColumn so that different selections have different background colors, or other attributes for that matter?

For example,

1) Red - Red background
2) Green - Green background
3) Yellow - Yellow background

Thanks,


Ken...

4 Answers, 1 is accepted

Sort by
0
Kenneth
Top achievements
Rank 1
answered on 28 Jun 2011, 04:01 PM
Figured it out.  An example is,

         comboBoxColumn.DataSource = new string[] {
            "",
            "<html><span style=\"background-color:yellow;\"><strong>NA</strong>                          </span>",
            "<html><span style=\"background-color:red;\"><strong>ANM</strong>                       </span>",
            "<html><span style=\"background-color:green;\"><strong>VAL</strong>                         </span>"
         };



Ken...
0
Alexander
Telerik team
answered on 29 Jun 2011, 01:18 PM
Hello Kenneth,

The approach you use handles this particular case. In general, you can define the visual appearance of RadDropDownListEditor items handling the VisualItemFormatting event of RadDropDownListEditor's EditorElement as demonstrates in the following code snippet:
this.radGridView1.CellEditorInitialized += new GridViewCellEventHandler(radGridView1_CellEditorInitialized);
 
private bool tbSubscribed = false;
private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
{
    if (tbSubscribed)
    {
        return;
    }
 
    RadDropDownListEditor editor = e.ActiveEditor as RadDropDownListEditor;
    if (editor != null)
    {
        RadDropDownListEditorElement editorElement = editor.EditorElement as RadDropDownListEditorElement;
        editorElement.VisualItemFormatting += new VisualListItemFormattingEventHandler(editorElement_VisualItemFormatting);
    }
}
 
private void editorElement_VisualItemFormatting(object sender, VisualItemFormattingEventArgs args)
{
    args.VisualItem.DrawFill = true;
    args.VisualItem.GradientStyle = GradientStyles.Solid;
 
    switch (args.VisualItem.Text)
    {
        case "Red":
            args.VisualItem.BackColor = Color.Red;
            break;
        case "Green":
            args.VisualItem.BackColor = Color.Green;
            break;
        case "Yellow":
            args.VisualItem.BackColor = Color.Yellow;
            break;
        default:
            args.VisualItem.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
            args.VisualItem.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
            args.VisualItem.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
            break;
    }
}

I hope it helps.

Best regards,
Alexander
the Telerik team
Q1’11 SP1 of RadControls for WinForms is available for download; also available is the Q2'11 Roadmap for Telerik Windows Forms controls.
0
Kenneth
Top achievements
Rank 1
answered on 29 Jun 2011, 02:46 PM
Thank you.  This does work better in the general case.

Would you please explain the purpose of the tbSubscribed bool variable?

It is obviously from a larger code snippet, but I don't see what purpose it has.  It doesn't appear to be applicable in this case.



Ken...
0
Alexander
Telerik team
answered on 04 Jul 2011, 03:54 PM
Hello Kenneth,

The boolean flag has its purpose in this solution. As the Handling Editors' events help article explains:

'The editors in RadGridView are reused, so we define a field which prevents us from subscribing to the KeyDown more than once.'

In our case it means that the RadDropDownListEditor is created when first needed and then reused for the other cells of RadGridView. The flag prevents unneeded additional event subscriptions.

Best regards,
Alexander
the Telerik team
Registration for Q2 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting July 18th and book your seat for a walk through all the exciting stuff we ship with the new release!
Tags
GridView
Asked by
Kenneth
Top achievements
Rank 1
Answers by
Kenneth
Top achievements
Rank 1
Alexander
Telerik team
Share this question
or