Column header color

19 posts, 0 answers
  1. Claudio
    Claudio avatar
    4 posts
    Member since:
    May 2012

    Posted 12 Aug 2013 Link to this post

    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

  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 13 Aug 2013 Link to this post

    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.
  3. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3626 posts

    Posted 15 Aug 2013 Link to this post

    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 >>
  4. Claudio
    Claudio avatar
    4 posts
    Member since:
    May 2012

    Posted 16 Aug 2013 Link to this post

    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;

  5. Claudio
    Claudio avatar
    4 posts
    Member since:
    May 2012

    Posted 19 Aug 2013 Link to this post

    Is there no way to get the current color of the cell?
  6. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3626 posts

    Posted 21 Aug 2013 Link to this post

    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 >>
  7. JAVI AYESA
    JAVI AYESA avatar
    4 posts
    Member since:
    Dec 2013

    Posted 13 Mar 2014 in reply to Princy Link to this post

    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
  8. JAVI AYESA
    JAVI AYESA avatar
    4 posts
    Member since:
    Dec 2013

    Posted 13 Mar 2014 Link to this post

    Sorry, did not read every post. I've already solved. thanks
  9. Ras Ran
    Ras Ran avatar
    134 posts
    Member since:
    Nov 2018

    Posted 12 Nov 2018 Link to this post

    how to change all raddatagrid column header color by using for loop ?
  10. andi
    andi avatar
    18 posts
    Member since:
    Oct 2018

    Posted 12 Nov 2018 in reply to Ras Ran Link to this post

    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

     

  11. Ras Ran
    Ras Ran avatar
    134 posts
    Member since:
    Nov 2018

    Posted 12 Nov 2018 in reply to andi Link to this post

    is it vb Code  Andi ?
  12. andi
    andi avatar
    18 posts
    Member since:
    Oct 2018

    Posted 12 Nov 2018 in reply to Ras Ran Link to this post

    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

  13. Ras Ran
    Ras Ran avatar
    134 posts
    Member since:
    Nov 2018

    Posted 12 Nov 2018 in reply to Ras Ran Link to this post

    i need c# code
  14. LEON
    LEON avatar
    13 posts
    Member since:
    Nov 2018

    Posted 12 Nov 2018 in reply to Ras Ran Link to this post

    http://converter.telerik.com/

    you can convert VB.net code to C# with this.
  15. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3626 posts

    Posted 13 Nov 2018 Link to this post

    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.
  16. Veena
    Veena avatar
    17 posts
    Member since:
    Jul 2019

    Posted 23 Jul 2019 in reply to Dess | Tech Support Engineer, Sr. Link to this post

    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?
  17. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3626 posts

    Posted 23 Jul 2019 Link to this post

    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.
  18. Veena
    Veena avatar
    17 posts
    Member since:
    Jul 2019

    Posted 23 Jul 2019 in reply to Dess | Tech Support Engineer, Sr. Link to this post

    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.

     

  19. Dimitar
    Admin
    Dimitar avatar
    21 posts

    Posted 24 Jul 2019 Link to this post

    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.
Back to Top