Is there a way to control a column header foreground and background individually? Meaning only change one the column headers to a different color text and background? i'm using GridViewTextBoxColumn in WinForms version 2012.2.912.40
Thanks
18 Answers, 1 is accepted
Please try the following code snippet I tried to change the fore color and background color of a column header.
C#:
private
void
radGridView1_ViewCellFormatting(
object
sender, CellFormattingEventArgs e)
{
if
(e.CellElement
is
GridHeaderCellElement)
{
if
(e.CellElement.Text ==
"Your header cell text"
)
//checking for the text in header cell
{
e.CellElement.DrawBorder =
true
;
e.CellElement.DrawFill =
true
;
e.CellElement.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
e.CellElement.BackColor = Color.Green;
e.CellElement.ForeColor = Color.Red;
}
}
}
Thanks,
Princy.
Thank you for contacting Telerik Support.
Princy's answer is in the right direction. The appropriate way to achieve your goal is to use the ViewCellFormatting event which is fired not only for data cells, but for all RadGridView cells.
Due to the UI virtualization in RadGridView, cell elements are created only for currently visible cells and are being reused during operations like scrolling, filtering, grouping and so on.
In order to prevent applying the formatting to other columns' cell elements (because of the cell reuse) all customization should be reset for the rest of the cell elements (please see the attached picture demonstrating wrong formatting without resetting):
private
void
radGridView1_ViewCellFormatting(
object
sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
{
if
(e.CellElement
is
GridHeaderCellElement)
{
if
(e.CellElement.Text ==
"City"
|| e.CellElement.Text ==
"Phone"
)
//checking for the text in header cell
{
e.CellElement.DrawBorder =
true
;
e.CellElement.DrawFill =
true
;
e.CellElement.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
e.CellElement.BackColor = Color.Green;
e.CellElement.ForeColor = Color.Red;
}
else
{
e.CellElement.ResetValue(LightVisualElement.DrawBorderProperty, ValueResetFlags.Local);
e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
e.CellElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
e.CellElement.ResetValue(LightVisualElement.ForeColorProperty, ValueResetFlags.Local);
}
}
}
More information about cell formatting is available here: http://www.telerik.com/help/winforms/gridview-cells-formatting-cells.html.
I hope this information helps. Should you have further questions, I would be glad to help.
Regards, Desislava
Telerik
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Once the color is applied, how do we get the color of the cell from the cell's rowindex and columnindex?
I have a CellDoubleClickevent set up on the grid which gives me an instance of CellFormattingEventArgs and I need to get to the cell's backcolor. I use the following code, but it doesn't give me the right color.
void CellDoubleClick(object sender, CellFormattingEventArgs e)
{
myOpenGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor
}
Note: myOpenGrid is of type RadGridView. BackColor2, BackColor3, BackColor4 doesn't work either.
Here's my code to set the backcolor in myOpenGrid_ViewCellFormatting event
e.CellElement.DrawFill = true;
e.CellElement.ForeColor = Color.Black;
e.CellElement.NumberOfColors = 1;
e.CellElement.BackColor = Color.LightBlue;
Thank you for writing back.
In order to get the cell BackColor in the CellDoubleClick event, it is appropriate to use the following code:
private
void
radGridView1_CellDoubleClick(
object
sender, GridViewCellEventArgs e)
{
GridHeaderCellElement headerCell = sender
as
GridHeaderCellElement;
GridDataCellElement cell = sender
as
GridDataCellElement;
Color color = Color.Transparent;
if
(headerCell !=
null
)
{
color = headerCell.BackColor;
}
else
if
(cell !=
null
)
{
color = cell.BackColor;
}
}
Note that
Color color =
this
.radGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor;
I hope this information helps. Should you have further questions, I would be glad to help.
Regards, Desislava
Telerik
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
I have a problem with that code. If I have a horizontal scroll in the grid, to commute with him the background colors of the headers are changed uncontrollably.
any ideas?
Thanks
i use this code without loop just assigned what the object is :
If (e.CellElement.ViewTemplate Is template) And (TypeOf e.CellElement Is GridHeaderCellElement) Then
e.CellElement.BackColor = Color.PowderBlue
e.CellElement.NumberOfColors = 1
e.CellElement.DrawFill = True
'e.CellElement.TableElement.TableHeaderHeight = 100
ElseIf (e.CellElement.ViewTemplate Is template) And (TypeOf e.CellElement Is GridCellElement) Then
e.CellElement.BackColor = Color.Honeydew
e.CellElement.NumberOfColors = 1
e.CellElement.DrawFill = True
End If
If (e.CellElement.ViewTemplate Is template1) And (TypeOf e.CellElement Is GridHeaderCellElement) Then
e.CellElement.BackColor = Color.DarkKhaki
e.CellElement.NumberOfColors = 1
e.CellElement.DrawFill = True
'e.CellElement.TableElement.TableHeaderHeight = 100
ElseIf (e.CellElement.ViewTemplate Is template1) And (TypeOf e.CellElement Is GridCellElement) Then
e.CellElement.BackColor = Color.LightGoldenrodYellow
e.CellElement.NumberOfColors = 1
e.CellElement.DrawFill = True
End If
yes it is. use ViewCellFormatting event
Private Sub RadGridView1_ViewCellFormatting(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.CellFormattingEventArgs) Handles RadGridView1.ViewCellFormatting
End Sub
you can convert VB.net code to C# with this.
In order to customize the grid cells, it is suitable to use the ViewCellFormatting event considering the type of the CellElement that you want to customize. Due to the UI virtualization in RadGridView, cell elements are created only for currently visible cells and are being reused during operations like scrolling, filtering, grouping and so on. In order to prevent applying the formatting to other columns' cell elements (because of the cell reuse), all customization should be reset for the rest of the cell elements. Additional information is available in the following help article: https://docs.telerik.com/devtools/winforms/gridview/cells/formatting-cells
You can use the spy tool in order to inspect the cell type that you want customize: https://docs.telerik.com/devtools/winforms/tools/controlspy/controlspy
I hope this information helps.
Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
In order to use RadGridView in your project and handle the CellFormatting event you need to ensure that the following references are added to your project:
-Telerik.WinControls
-Telerik.WinControls.GridView
-Telerik.WinControls.UI
-TelerikCommon
I hope this information helps.
Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
If I search for Telerik.WinControls in Telerik nuget packages list, I am not getting any result. could you please tell me the exact nuget package name? I already have installed 4 nuget packages as in the attached screenshot.
Based on your screenshot it seems that you have installed WPF NuGet packages in your project.
Please note that Desislava's message is valid for UI for WinForms not WPF since this forum is related to the Telerik UI for WinForms suite.
Can you please confirm whether you are using WPF or WinForms technology so we can assist you further?
Regards,
Dimitar
Progress Telerik