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

Column header color

18 Answers 440 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Claudio
Top achievements
Rank 1
Claudio asked on 12 Aug 2013, 12:31 PM

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

Sort by
0
Princy
Top achievements
Rank 1
answered on 13 Aug 2013, 09:27 AM
Hi Claudio,

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.
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 15 Aug 2013, 11:52 AM
Hello Claudio,

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
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
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 >>
0
Claudio
Top achievements
Rank 1
answered on 16 Aug 2013, 03:58 PM
Thanks, this helped.

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;

0
Claudio
Top achievements
Rank 1
answered on 19 Aug 2013, 07:58 PM
Is there no way to get the current color of the cell?
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 21 Aug 2013, 01:10 PM
Hello Claudio,

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;
may throw an exception (ThrowArgumentOutOfRangeException) if you click on the header cell as its RowIndex=-1. In general you should use avoid using indexes in order to lower the chances of such errors.

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
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 >>
0
JAVI AYESA
Top achievements
Rank 1
answered on 13 Mar 2014, 02:18 PM
Hello Princy,

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
0
JAVI AYESA
Top achievements
Rank 1
answered on 13 Mar 2014, 02:26 PM
Sorry, did not read every post. I've already solved. thanks
0
Ras Ran
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 12 Nov 2018, 08:17 AM
how to change all raddatagrid column header color by using for loop ?
0
andi
Top achievements
Rank 1
answered on 12 Nov 2018, 08:46 AM

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

 

0
Ras Ran
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 12 Nov 2018, 09:02 AM
is it vb Code  Andi ?
0
andi
Top achievements
Rank 1
answered on 12 Nov 2018, 09:04 AM

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

0
Ras Ran
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 12 Nov 2018, 11:10 AM
i need c# code
0
LEON
Top achievements
Rank 1
answered on 12 Nov 2018, 08:04 PM
http://converter.telerik.com/

you can convert VB.net code to C# with this.
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 13 Nov 2018, 11:03 AM
Hello,  

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
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Veena
Top achievements
Rank 1
answered on 23 Jul 2019, 09:26 AM
Hi, I tried to use the same code for cell formatting but it says could not find Telerik.WinControls.UI.. may I know which nuget package I have to install for this?
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 23 Jul 2019, 12:50 PM
Hello, Praveena,  

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
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Veena
Top achievements
Rank 1
answered on 23 Jul 2019, 01:18 PM

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.

 

0
Dimitar
Telerik team
answered on 24 Jul 2019, 08:37 AM
Hello Praveena,

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
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
GridView
Asked by
Claudio
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Claudio
Top achievements
Rank 1
JAVI AYESA
Top achievements
Rank 1
Ras Ran
Top achievements
Rank 2
Iron
Veteran
Iron
andi
Top achievements
Rank 1
LEON
Top achievements
Rank 1
Veena
Top achievements
Rank 1
Dimitar
Telerik team
Share this question
or