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

Cell color duplicates

3 Answers 39 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Svein Thomas
Top achievements
Rank 1
Svein Thomas asked on 04 Mar 2011, 12:22 PM
Hi, 

I have a function that calculates the color based on value in a cell.
This works nicely, but when i scroll, the cell color duplicates and attaches to cells that should not have the color.

Please look at the sample code:

namespace RadControlsWinFormsApp3
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components;
 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #region Windows Form Designer generated code
 
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.radGridView1 = new Telerik.WinControls.UI.RadGridView();
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).BeginInit();
            this.SuspendLayout();
            //
            // radGridView1
            //
            this.radGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.radGridView1.Location = new System.Drawing.Point(0, 0);
            this.radGridView1.Name = "radGridView1";
            this.radGridView1.Size = new System.Drawing.Size(364, 395);
            this.radGridView1.TabIndex = 0;
            this.radGridView1.Text = "radGridView1";
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(364, 395);
            this.Controls.Add(this.radGridView1);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).EndInit();
            this.ResumeLayout(false);
 
        }
 
        #endregion
 
        private Telerik.WinControls.UI.RadGridView radGridView1;
    }
}


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
namespace RadControlsWinFormsApp3
{
    public partial class Form1 : Form
    {
        List<RowHelper> rows = new List<RowHelper>();
        colorhelper MyColors = new colorhelper();
        public Form1()
        {
            InitializeComponent();
            Telerik.WinControls.UI.GridViewDecimalColumn gridViewDecimalColumn1 = new Telerik.WinControls.UI.GridViewDecimalColumn();
            Telerik.WinControls.UI.GridViewTextBoxColumn gridViewTextBoxColumn1 = new Telerik.WinControls.UI.GridViewTextBoxColumn();
            Telerik.WinControls.UI.GridViewComboBoxColumn gridViewComboBoxColumn1 = new Telerik.WinControls.UI.GridViewComboBoxColumn();
            Telerik.WinControls.UI.GridViewMultiComboBoxColumn gridViewComboBoxColumn2 = new Telerik.WinControls.UI.GridViewMultiComboBoxColumn();
 
 
 
            rows.Add(new RowHelper(1, "Row1", 1, 200000));
            rows.Add(new RowHelper(2, "Row2", 2, 400000));
            rows.Add(new RowHelper(3, "Row3", 3, 600000));
            rows.Add(new RowHelper(4, "Row4", 1, 1200000));
            rows.Add(new RowHelper(5, "Row4", 1, 1800000));
            rows.Add(new RowHelper(6, "Row4", 1, null));
            rows.Add(new RowHelper(7, "Row4", 1, null));
            rows.Add(new RowHelper(8, "Row4", 1, null));
            rows.Add(new RowHelper(9, "Row4", 1, null));
            rows.Add(new RowHelper(10, "Row4", 1, null));
            rows.Add(new RowHelper(11, "Row4", 1, null));
            rows.Add(new RowHelper(12, "Row4", 1, null));
            rows.Add(new RowHelper(13, "Row4", 1, null));
            rows.Add(new RowHelper(14, "Row4", 1, null));
            rows.Add(new RowHelper(15, "Row4", 1, null));
            rows.Add(new RowHelper(16, "Row4", 1, null));
            rows.Add(new RowHelper(17, "Row4", 1, null));
            rows.Add(new RowHelper(18, "Row4", 1, null));
            rows.Add(new RowHelper(19, "Row4", 1, null));
            rows.Add(new RowHelper(20, "Row4", 1, null));
            rows.Add(new RowHelper(21, "Row4", 1, null));
            rows.Add(new RowHelper(22, "Row4", 1, null));
            rows.Add(new RowHelper(23, "Row4", 1, null));
            rows.Add(new RowHelper(24, "Row4", 1, null));
            rows.Add(new RowHelper(25, "Row4", 1, null));
 
            List<helper> list = new List<helper>();
            list.Add(new helper(1, "test1"));
            list.Add(new helper(2, "test2"));
            list.Add(new helper(3, "test3"));
 
            List<multicolumnhelper> multicolumnlist = new List<multicolumnhelper>();
            multicolumnlist.Add(new multicolumnhelper(1, "test1", "col2"));
            multicolumnlist.Add(new multicolumnhelper(2, "test2", "col2"));
            multicolumnlist.Add(new multicolumnhelper(3, "test3", "col2"));
 
            gridViewComboBoxColumn1.DataSource = list;
            gridViewComboBoxColumn1.DisplayMember = "Value";
            gridViewComboBoxColumn1.ValueMember = "ID";
            gridViewComboBoxColumn1.HeaderText = "Test";
            gridViewComboBoxColumn1.Name = "test";
            gridViewComboBoxColumn1.FieldName = "RowID";
 
            gridViewTextBoxColumn1.Name = "rowtext";
            gridViewTextBoxColumn1.HeaderText = "rowtext";
            gridViewTextBoxColumn1.FieldName = "RowText";
 
 
            gridViewComboBoxColumn2.DataSource = multicolumnlist;
            gridViewComboBoxColumn2.DisplayMember = "Value";
            gridViewComboBoxColumn2.ValueMember = "ID";
            gridViewComboBoxColumn2.HeaderText = "Test";
            gridViewComboBoxColumn2.Name = "test2";
            gridViewComboBoxColumn2.FieldName = "RowID";
 
            gridViewDecimalColumn1.Name = "ColorTest";
            gridViewDecimalColumn1.HeaderText = "ColorTest";
            gridViewDecimalColumn1.FieldName = "ColorTest";
 
 
            radGridView1.Columns.AddRange(new Telerik.WinControls.UI.GridViewDataColumn[] { gridViewComboBoxColumn1, gridViewTextBoxColumn1, gridViewComboBoxColumn2, gridViewDecimalColumn1 });
            radGridView1.CellFormatting += new CellFormattingEventHandler(radGridView1_CellFormatting);
            PopulateData();
 
            this.radGridView1.CellEditorInitialized += new GridViewCellEventHandler(radGridView1_CellEditorInitialized);
 
        }
 
        void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            if (e.Column.Name == "ColorTest")
            {
                if (e.CellElement.Value != null)
                {
                    e.CellElement.DrawFill = true;
                    try
                    {
 
                        Color useColor = MyColors.GetColorForValue((int)e.CellElement.Value);
                        e.CellElement.BackColor = useColor;
                        e.CellElement.BackColor2 = useColor;
                        e.CellElement.BackColor3 = useColor;
                        e.CellElement.BackColor4 = useColor;
                    }
                    catch (Exception)
                    {
 
                      
                    }
                }
            }
        }
 
        void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
        {
            if (e.ActiveEditor is RadMultiColumnComboBoxElement)
            {
                RadMultiColumnComboBoxElement editor = (RadMultiColumnComboBoxElement)e.ActiveEditor;
                editor.DropDownWidth = 200;
                editor.EditorControl.ShowColumnHeaders = false;
                editor.EditorControl.Columns["ID"].IsVisible = false;
                editor.EditorControl.ShowRowHeaderColumn = false;
            }
        }
 
        private void PopulateData()
        {
            radGridView1.DataSource = rows;
        }
 
         
    }
 
    public class colorhelper
    {
        public colors DefaultColor = null;
        List<colors> myColors = new List<colors>();
        public colorhelper()
        {
             
            myColors.Add(new colors(0, 300000, Color.Green, Color.Green));
            myColors.Add(new colors(300000, 500000, Color.Green, Color.Yellow));
            myColors.Add(new colors(500000, 1200000, Color.Yellow, Color.Red));
            DefaultColor = myColors[2];
        }
 
 
        public Color GetColorForValue(decimal Value)
        {
            colors col = myColors.Find(c => c.From <= Value && c.To >= Value);
            if (col == null) col = DefaultColor;
            return col.GetColor(Value);
        }
 
        public class colors{
 
            public colors(int from, int to, Color fromcolor, Color tocolor)
            {
                From = from;
                To = to;
                FromColor = fromcolor;
                ToColor = tocolor;
            }
 
            public int From { get; set; }
            public int To { get; set; }
            public Color FromColor { get; set; }
            public Color ToColor { get; set; }
 
            
            public int size
            {
                get { return To - From; }
            }
 
            public int relativeValue(decimal value)
            {
                if (value > To)
                    return 0;
                else
                    return To - (int)value;
            }
 
            public Color GetColor(decimal Value)
            {
                int rMax = FromColor.R;
                int rMin = ToColor.R;
                int gMax = FromColor.G;
                int gMin = ToColor.G;
                int bMax = FromColor.B;
                int bMin = ToColor.B;
 
                var rAverage = Math.Abs(rMin + (int)((rMax - rMin) * relativeValue(Value) / size));
                var gAverage = Math.Abs(gMin + (int)((gMax - gMin) * relativeValue(Value) / size));
                var bAverage = Math.Abs(bMin + (int)((bMax - bMin) * relativeValue(Value) / size));
 
                return Color.FromArgb(rAverage, gAverage, bAverage);
            }
        }
    }
 
    public class helper
    {
        public helper(int id, string value)
        {
            ID = id;
            Value = value;
        }
 
        public int ID { get; set; }
        public string Value { get; set; }
    }
 
    public class multicolumnhelper
    {
        public multicolumnhelper(int id, string value, string value2)
        {
            ID = id;
            Value = value;
            Value2 = value2;
        }
 
        public int ID { get; set; }
        public string Value { get; set; }
        public string Value2 { get; set; }
 
 
 
    }
    public class RowHelper
    {
        public RowHelper(int id, string text, int id2, int? value)
        {
            RowID = id;
            RowText = text;
            RowID2 = id2;
            ColorTest = value;
        }
 
        public int RowID { get; set; }
        public string RowText { get; set; }
        public int RowID2 { get; set; }
        public int? ColorTest { get; set; }
 
 
    }
 
}

3 Answers, 1 is accepted

Sort by
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 04 Mar 2011, 12:31 PM
Hello,

The reason for this is because in your CellFormatting event you need to reset the value if it does not meet your condition. This is because the RadGridView uses UI virtualization to effectively re-use cell as you scroll up and down the grid.

For information on resetting the values in the CellFormatting event, please see this help topic

If you need further assistance, please let me know
Thanks
Richard
0
Svein Thomas
Top achievements
Rank 1
answered on 04 Mar 2011, 12:36 PM
Thanks Richard !
0
Richard Slade
Top achievements
Rank 2
answered on 04 Mar 2011, 12:40 PM
No problem. Glad to be able to help
Richard
Tags
GridView
Asked by
Svein Thomas
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Svein Thomas
Top achievements
Rank 1
Share this question
or