Setting the background color of a cell

2 posts, 1 answers
  1. Paul
    Paul avatar
    6 posts
    Member since:
    Feb 2012

    Posted 29 Feb 2012 Link to this post

     I found this link which shows some sample code on how to set the background color of a cell. The selectionmode is FulLRowSelect. I have some code in an event handler for CellFormatting that does this:

    if (someConditionIsTrue)
    {
    e.CellElement.BackColor = Color.Red;
    return;
    }

    e.CellElement.ResetValue(LightVisualElement.BackColorProperty, Telerik.WinControls.ValueResetFlags.Local);


    when certain conditions are met. At first I didn't think it was working, but I noticed when I moved my mouse over a row that should have a cell with a red background, suddenly the background switched to red. As soon as I move my mouse off that row it switches back to it's default color. That's not the behavior I'd like - it should be red regardless of whether the mouse is over that row or not.

    What am I missing?
  2. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 01 Mar 2012 Link to this post

    Hello Paul,

    You also need to set the DrawFill property to true to make it stay on. The exmaple below assumes you have a form with a RadGridView on it.
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using Telerik.WinControls;
    using Telerik.WinControls.UI;
     
    namespace RadControlsWinFormsApp1
    {
        public partial class RadForm1 : Telerik.WinControls.UI.RadForm
        {
            public RadForm1()
            {
                InitializeComponent();
     
                List<User> users = new List<User>();
                users.Add(new User(1, "Richard"));
                users.Add(new User(2, "Bob"));
                users.Add(new User(3, "Chris"));
                users.Add(new User(4, "Stewart"));
     
                this.radGridView1.SelectionMode = Telerik.WinControls.UI.GridViewSelectionMode.FullRowSelect;
     
                this.radGridView1.DataSource = users;
                this.radGridView1.ReadOnly = true;
                this.radGridView1.CellFormatting += new Telerik.WinControls.UI.CellFormattingEventHandler(radGridView1_CellFormatting);
            }
     
            void radGridView1_CellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
            {
                if (e.Column.Name == "Id" && e.RowIndex > -1)
                {
                    if (e.CellElement.Value.ToString() == "2")
                    {
                        e.CellElement.BackColor = System.Drawing.Color.Red;
                        e.CellElement.NumberOfColors = 1;
                        e.CellElement.DrawFill = true;
                    }
                    else
                    {
                        e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
                        e.CellElement.ResetValue(LightVisualElement.NumberOfColorsProperty, ValueResetFlags.Local);
                        e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
                    }
                }
                else
                {
                    e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
                    e.CellElement.ResetValue(LightVisualElement.NumberOfColorsProperty, ValueResetFlags.Local);
                    e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
                }
            }
        }
     
        public class User
        {
            public User(int id, string name)
            {
                Id = id;
                Name = name;
            }
     
            public User()
            { }
     
            public int Id
            {
                get;
                set;
            }
     
            public string Name
            {
                get;
                set;
            }
        }
    }

    Hope that helps, but let me know if you need further information
    Richard
Back to Top