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
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
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
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
With the ViewCellFormating Event it works.
Thanks
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
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.
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!
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!