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

Group row coloring

10 Answers 351 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Brennan
Top achievements
Rank 1
Brennan asked on 10 Nov 2010, 04:22 PM
Is it possible to color each group differently?  For example, if I were grouping my grid by Age and Gender could I make the header rows of Age red and the header rows of Gender blue?  (Note: Not the rows of the actual data being grouped, just the headers)

Also, is it possible to add text to row headers?  For example, if I was grouping by department number, could I put the departments total income in the grouping header?

10 Answers, 1 is accepted

Sort by
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 10 Nov 2010, 06:04 PM
Hello Brennan,

Yes it is possible, but let's take them one by one.
The first one, Custom coloring for group rows, you can register for the ViewCellFormatting event and do the following
void radGridView1_ViewCellFormatting(object sender, CellFormattingEventArgs e)
{
    var groupContentCellElement = sender as GridGroupContentCellElement;
    if (groupContentCellElement != null)
    {
        groupContentCellElement.DrawFill = true;
        groupContentCellElement.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
        if (groupContentCellElement.RowIndex % 2 == 0)
        {
            groupContentCellElement.BackColor = Color.LightGoldenrodYellow;
        }
        else
        {
            groupContentCellElement.BackColor = Color.LightBlue;
        }
    }
}

After this, if you want to change the text displayed in the GroupRowHeader, you can register to the GroupSummaryEvaluate event and add the required text to the FormatString property, like so:
void radGridView1_GroupSummaryEvaluate(object sender, GroupSummaryEvaluationEventArgs e)
{
    e.FormatString = e.FormatString + " And some other text";
}

And if you want to see all of these in action please take a look at the following full and working example:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Telerik.WinControls.Data;
using Telerik.WinControls.UI;
 
public partial class Form1 : Form
{
    private RadGridView radGridView1;
 
    public Form1()
    {
        InitializeComponent();
        this.Controls.Add(radGridView1 = new RadGridView());
        radGridView1.Dock = DockStyle.Fill;
        this.radGridView1.DataBindingComplete += new GridViewBindingCompleteEventHandler(radGridView1_DataBindingComplete);
 
        this.radGridView1.GroupDescriptors.Add(new Telerik.WinControls.Data.GroupDescriptor(new SortDescriptor("Id", ListSortDirection.Ascending, null)));
        radGridView1.CellFormatting += new CellFormattingEventHandler(radGridView1_CellFormatting);
        radGridView1.ViewCellFormatting += new CellFormattingEventHandler(radGridView1_ViewCellFormatting);
        radGridView1.GroupSummaryEvaluate += new GroupSummaryEvaluateEventHandler(radGridView1_GroupSummaryEvaluate);
    }
 
    void radGridView1_GroupSummaryEvaluate(object sender, GroupSummaryEvaluationEventArgs e)
    {
        e.FormatString = e.FormatString + " And some other text";
    }
 
    void radGridView1_ViewCellFormatting(object sender, CellFormattingEventArgs e)
    {
        var groupContentCellElement = sender as GridGroupContentCellElement;
        if (groupContentCellElement != null)
        {
            groupContentCellElement.DrawFill = true;
            groupContentCellElement.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
            if (groupContentCellElement.RowIndex % 2 == 0)
            {
                groupContentCellElement.BackColor = Color.LightGoldenrodYellow;
            }
            else
            {
                groupContentCellElement.BackColor = Color.LightBlue;
            }
        }
    }
 
    void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
    {
    }
 
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        this.radGridView1.DataSource = new ProductsCollection(10);
    }
 
    void radGridView1_DataBindingComplete(object sender, GridViewBindingCompleteEventArgs e)
    {
        radGridView1.Columns["BuyerId"].IsVisible = false;
 
        var column = new GridViewComboBoxColumn("SomeComboboxColumn", "SomeComboboxColumn");
        column.DataSource = new BuyersCollection(10);
        column.ValueMember = "Id";
        column.FieldName = "BuyerId";
        column.DisplayMember = "Name";
        this.radGridView1.Columns.Add(column);
 
        this.radGridView1.BestFitColumns();
    }
}
 
#region Helpers
 
public class Product
{
    public int Id
    {
        get;
        set;
    }
 
    public int BuyerId
    {
        get;
        set;
    }
 
    public Product()
    {
    }
 
    public Product(int id, int buyerId)
    {
        this.Id = id;
        this.BuyerId = buyerId;
    }
}
 
public class Buyer
{
    public int Id
    {
        get;
        set;
    }
 
    public string Name
    {
        get;
        set;
    }
 
    public Buyer(int id, string name)
    {
        this.Id = id;
        this.Name = name;
    }
}
 
public class ProductsCollection : BindingList<Product>
{
    public ProductsCollection(int noItems)
    {
        for (int i = 0; i < noItems; i++)
        {
            this.Add(new Product(i, i + 10));
        }
    }
}
 
public class BuyersCollection : BindingList<Buyer>
{
    public BuyersCollection(int startIndex)
    {
        for (int i = startIndex; i < startIndex + 10; i++)
        {
            this.Add(new Buyer(i, "Buyer " + (i + 1).ToString()));
        }
    }
}
 
#endregion Helpers

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
0
Brennan
Top achievements
Rank 1
answered on 10 Nov 2010, 06:54 PM
Thanks man, you're a pro.
0
Filleau
Top achievements
Rank 1
answered on 04 May 2011, 07:08 PM
Hi

I'm trying to give some color too

Trying in vb

 Private Sub RadGridViewDFS_CellFormatting(ByVal sender As System.Object, ByVal e As Telerik.WinControls.UI.CellFormattingEventArgs) Handles RadGridViewDFS.CellFormatting
        Dim groupContentCellElement = TryCast(sender, GridGroupContentCellElement)
        If groupContentCellElement IsNot Nothing Then
            groupContentCellElement.DrawFill = True
            groupContentCellElement.GradientStyle = Telerik.WinControls.GradientStyles.Solid
            If groupContentCellElement.RowIndex Mod 2 = 0 Then
                groupContentCellElement.BackColor = Color.LightGoldenrodYellow
            Else
                groupContentCellElement.BackColor = Color.LightBlue
            End If
        End If

    End Sub




The event fire well but each time the sender is typeof griddatacellelement


I use the blackoffice skin and the +/- row with the group cell is not enougth constrated. So I want to have a backcolor or forecolor on theses rows.

Can you help me ?

Thanlks
0
Stefan
Telerik team
answered on 09 May 2011, 04:16 PM
Hello Filleau,

Thank you for writing.

CellFormatting is triggered for data cells only, while ViewCellFormatting (as in the example below) is triggered for all cells. Please refer to this help article.

I hope this helps.
 
Kind regards,
Stefan
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
Filleau
Top achievements
Rank 1
answered on 09 May 2011, 04:22 PM
Ho my god ! This I made an error in the event name...

With the ViewCellFormating Event it works.

Thanks
0
Stefan
Telerik team
answered on 12 May 2011, 07:36 AM
Hi Filleau,

I am glad to hear that you have solved this. Should you need any other assistance, do not hesitate to contact us.
 
Kind regards,
Stefan
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
Mangesh
Top achievements
Rank 1
answered on 20 Jul 2011, 01:34 PM
I am using ViewCellFormatting event for row formatting.problem is that i am using realtime data feed so its updating again and again and viewcell event is firing more then onece means every time data is updated.I want to fire this event only startup of grid binding.let me know the solution for this or is there any way to run according to user need.
0
Svett
Telerik team
answered on 25 Jul 2011, 02:02 PM
Hello Mangesh,

The CellFormatting/ViewCellformatting events are fired very often, when a UI or data operation has been performed. There is no work around to prevent the events from being so many times.

Could you give us more details about your scenario requirements? Hence, we will be able to understand your case in depth and give you an appropriate solution.

Greetings,
Svett
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Mangesh
Top achievements
Rank 1
answered on 25 Jul 2011, 02:11 PM
Yes you are right about firing viewcell formatting event.But i think you must be aware about real time data according to forex market as it fluctuate in every 1 seconds.Then when new data will come Grid View will update and Viewcell event will fire.means viewcell event fire when rate will fluctuate.Then is it possible that i can fire viewcell event according to my need or is there any other event.Actually i am planning to purchase Telerik Ultimate collection but as per my knowlwedge Telerik Grid view is unable to handle Real Time data Please help me about realtime Data and how it will update whenever bew data will come.
0
Svett
Telerik team
answered on 28 Jul 2011, 08:40 AM
Hi Mangesh,

Another alternative of the ViewCellFormatting/CellFormatting events is the usage of the cell's style property. You can read more about that in this blog post. Nevertheless, I would kindly ask you to open a support ticket. There you can send us a sample project which demonstrates your approach. This will help us to understand it in depth. Hence, we will able to debug your application and figure out the performance issues. 

Best wishes,
Svett
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Tags
GridView
Asked by
Brennan
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Brennan
Top achievements
Rank 1
Filleau
Top achievements
Rank 1
Stefan
Telerik team
Mangesh
Top achievements
Rank 1
Svett
Telerik team
Share this question
or