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

Alternating colors for rows within a column group

11 Answers 241 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Deepak
Top achievements
Rank 1
Deepak asked on 02 Feb 2011, 06:16 PM
Is there a way to get that using ColumnGroupsViewDefinition. I have a Columngroup with 3 rows in them and I want each of them to have a distinct color.

Thanks
Deepak

11 Answers, 1 is accepted

Sort by
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 02 Feb 2011, 09:25 PM
Hello Deepak,

Hope you're well. Perhaps you are referring to the issue at this PITS Link?
If not, please include a screenshot of what you wish to do and I'll do my best to help
Thanks
Richard
0
Deepak
Top achievements
Rank 1
answered on 03 Feb 2011, 10:17 AM
Hello Richard,
       Good to hear from you. I hope you are doing well too... The issue is not the one in PITS. We are trying to move from using Infragistics grid to using a Telerik grid. I have attached the existing Infragistics grid as well as what I got with Telerik columngroupsviewdefinition.

Thanks
Deepak
0
Richard Slade
Top achievements
Rank 2
answered on 03 Feb 2011, 11:47 AM
Hi Deepak,

I think I understand what you need. Please can you try the code below and let me know if that's ok for you.

Designer File
namespace RadGridView_ColumnGroupView_C
{
    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(523, 440);
            this.radGridView1.TabIndex = 0;
            this.radGridView1.Text = "radGridView1";
            this.radGridView1.RowFormatting += new Telerik.WinControls.UI.RowFormattingEventHandler(this.radGridView1_RowFormatting);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(523, 440);
            this.Controls.Add(this.radGridView1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).EndInit();
            this.ResumeLayout(false);
  
        }
  
        #endregion
  
        private Telerik.WinControls.UI.RadGridView radGridView1;
    }
}

form1.cs
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;
using Telerik.WinControls.UI;
  
namespace RadGridView_ColumnGroupView_C
{
    public partial class Form1 : Form
    {
        private List<MyObject> m_List = new List<MyObject>();
  
        public Form1()
        {
            InitializeComponent();
        }
  
        private void Form1_Load(object sender, EventArgs e)
        {
            CreateDataSource();
            this.radGridView1.DataSource = m_List;
  
            ColumnGroupsViewDefinition view = new ColumnGroupsViewDefinition();
            view.ColumnGroups.Add(new GridViewColumnGroup("Customer Contact"));
  
            view.ColumnGroups[0].Rows.Add(new GridViewColumnGroupRow());
            view.ColumnGroups[0].Rows[0].Columns.Add(this.radGridView1.Columns["Name"]);
            view.ColumnGroups[0].Rows[0].Columns.Add(this.radGridView1.Columns["Description"]);
            view.ColumnGroups[0].Rows[0].Columns.Add(this.radGridView1.Columns["Id"]);
  
  
            this.radGridView1.ViewDefinition = view;
  
            this.radGridView1.EnableAlternatingRowColor = false;
  
  
        }
  
        private void CreateDataSource()
        {
            m_List.Clear();
            for (int i = 1; i <= 50; i++)
            {
                m_List.Add(new MyObject("Name " + i.ToString(), "Description " + i.ToString(), i));
                m_List.Add(new MyObject("Name " + i.ToString(), "Description " + i.ToString(), i));
                m_List.Add(new MyObject("Name " + i.ToString(), "Description " + i.ToString(), i));
            }
  
        }
  
        private void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e)
        {
            if ((Convert.ToInt32(e.RowElement.RowInfo.Cells["Id"].Value) % 2) == 0)
            {
                e.RowElement.BackColor = Color.LightGreen;
                e.RowElement.NumberOfColors = 1;
                e.RowElement.DrawFill = true;
            }
            else
            {
                e.RowElement.ResetValue(LightVisualElement.BackColorProperty);
                e.RowElement.ResetValue(LightVisualElement.NumberOfColorsProperty);
                e.RowElement.ResetValue(LightVisualElement.DrawFillProperty);
            }
        }
  
    }
  
    public class MyObject
    {
        public MyObject()
        {}
  
        public MyObject(string name, string description, int id)
        {
            this.Name = name;
            this.Description = description;
            this.Id = id;
        }
  
        public string Name
        {get; set;}
  
        public string Description
        {get; set;}
  
        public int Id
        {get; set;}
    }
}

Regards
Richard
0
Deepak
Top achievements
Rank 1
answered on 03 Feb 2011, 11:57 AM
Hello Richard,
 Thanks for your reply. However I want Name, Description and Id in seperate rows within the same column group and each of those rows having a different colour within the column group.

Thanks
Deepak
0
Richard Slade
Top achievements
Rank 2
answered on 03 Feb 2011, 12:00 PM
Hi Deepak,

Sorry, I'm not understanding correctly what you mean when you say that you want Name, Description and Id on different rows. Please could you explain further. Apologies for the inconvenience.
Richard
0
Deepak
Top achievements
Rank 1
answered on 03 Feb 2011, 01:32 PM
Hi Richard,
 Sorry for the late reply... If you see the screenshot, there are 2 values for the age columns. For eg - Paris RP has 47 and 18.88 for 18-34; 18 and 11.69 for the next and 63 and 23.42 for the other. I want the rows containg 47, 18 and 63 to be one color and the row containing 18.88, 11.69 and 23.42 in another. The scheme being applied through out the grid

Thanks
Deepak
0
Richard Slade
Top achievements
Rank 2
answered on 03 Feb 2011, 03:08 PM
Hi Deepak,

Thanks for that. i'll come back to you as soon as I can
regards,
Richard
0
Deepak
Top achievements
Rank 1
answered on 03 Feb 2011, 03:08 PM
great... Thanks Richard
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 03 Feb 2011, 03:34 PM
Hi Again,

Please try this. I beleive that this is the type of thing you need.

Designer File
namespace RadGridView_ColumnGroupView_C
{
    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(523, 440);
            this.radGridView1.TabIndex = 0;
            this.radGridView1.Text = "radGridView1";
            this.radGridView1.CellFormatting += new Telerik.WinControls.UI.CellFormattingEventHandler(this.radGridView1_CellFormatting);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(523, 440);
            this.Controls.Add(this.radGridView1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).EndInit();
            this.ResumeLayout(false);
  
        }
  
        #endregion
  
        private Telerik.WinControls.UI.RadGridView radGridView1;
    }
}

Form1.cs
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;
using Telerik.WinControls.UI;
  
namespace RadGridView_ColumnGroupView_C
{
    public partial class Form1 : Form
    {
        private List<MyObject> m_List = new List<MyObject>();
  
        public Form1()
        {
            InitializeComponent();
        }
  
        private ColumnGroupsViewDefinition columnGroupsView;
        private void Form1_Load(object sender, EventArgs e)
        {
            CreateDataSource();
            this.radGridView1.DataSource = m_List;
  
            ColumnGroupsViewDefinition view = new ColumnGroupsViewDefinition();
            view.ColumnGroups.Add(new GridViewColumnGroup("Customer Contact"));
  
            view.ColumnGroups[0].Rows.Add(new GridViewColumnGroupRow());
            view.ColumnGroups[0].Rows[0].Columns.Add(this.radGridView1.Columns["Description"]);
  
            view.ColumnGroups[0].Rows.Add(new GridViewColumnGroupRow());
            view.ColumnGroups[0].Rows[1].Columns.Add(this.radGridView1.Columns["Name"]);
            view.ColumnGroups[0].Rows[1].Columns.Add(this.radGridView1.Columns["Title"]);
  
            view.ColumnGroups[0].Rows.Add(new GridViewColumnGroupRow());
            view.ColumnGroups[0].Rows[2].Columns.Add(this.radGridView1.Columns["Id"]);
  
            this.radGridView1.ViewDefinition = view;
  
            this.radGridView1.EnableAlternatingRowColor = false;
            this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
  
        }
  
        private void CreateDataSource()
        {
            m_List.Clear();
            for (int i = 1; i <= 50; i++)
            {
                m_List.Add(new MyObject("Name " + i.ToString(), "Description " + i.ToString(), i, "Mr"));
                m_List.Add(new MyObject("Name " + i.ToString(), "Description " + i.ToString(), i, "Miss"));
                m_List.Add(new MyObject("Name " + i.ToString(), "Description " + i.ToString(), i, "Dr"));
            }
  
        }
  
        private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            if (e.CellElement is GridDataCellElement && e.CellElement.RowIndex > -1)
            {
                if (e.CellElement.ColumnInfo.Name == "Description")
                {
                    e.CellElement.BackColor = Color.LightGreen;
                    e.CellElement.NumberOfColors = 1;
                    e.CellElement.DrawFill = true;
                }
                else if (e.CellElement.ColumnInfo.Name == "Name" | e.CellElement.ColumnInfo.Name == "Title")
                {
                    e.CellElement.BackColor = Color.LightPink;
                    e.CellElement.NumberOfColors = 1;
                    e.CellElement.DrawFill = true;  
                }
                else if (e.CellElement.ColumnInfo.Name == "Id")
                {
                    e.CellElement.BackColor = Color.LightSkyBlue;
                    e.CellElement.NumberOfColors = 1;
                    e.CellElement.DrawFill = true;
                }
                else
                {
                    e.CellElement.ResetValue(LightVisualElement.BackColorProperty);
                    e.CellElement.ResetValue(LightVisualElement.NumberOfColorsProperty);
                    e.CellElement.ResetValue(LightVisualElement.DrawFillProperty);
                }
            }
        }
  
    }
  
    public class MyObject
    {
        public MyObject()
        {}
  
        public MyObject(string name, string description, int id, string title)
        {
            this.Name = name;
            this.Description = description;
            this.Id = id;
            this.Title = title;
        }
  
        public string Name
        {get; set;}
  
        public string Description
        {get; set;}
  
        public int Id
        {get; set;}
  
        public string Title
        { get; set; }
    }
}

Hope that helps
Richard
0
Deepak
Top achievements
Rank 1
answered on 03 Feb 2011, 04:41 PM
Richard, You are the best...


Thanks
Deepak
0
Richard Slade
Top achievements
Rank 2
answered on 03 Feb 2011, 04:43 PM
Fantastic! - I'm glad that we've got a solution for you.
All the best
Richard
Tags
GridView
Asked by
Deepak
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Deepak
Top achievements
Rank 1
Share this question
or