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

Setting the background color of a cell

1 Answer 400 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Paul
Top achievements
Rank 1
Paul asked on 29 Feb 2012, 08:58 PM
 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?

1 Answer, 1 is accepted

Sort by
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 01 Mar 2012, 10:30 AM
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
Tags
GridView
Asked by
Paul
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Share this question
or