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

Header back color

7 Answers 172 Views
GridView
This is a migrated thread and some comments may be shown as answers.
FilipN
Top achievements
Rank 1
FilipN asked on 21 Aug 2009, 10:55 AM
Hello, 
I am trying to change color of the header cell in currently selected column. I tried to achieve this as it is done in this post Change of back color, however the header cells will start to react only if the user hovers with mouse over specified columns before he starts clicking into the grid. 

Here is the code snippet I use
        void electionChanged(object sender, EventArgs e)
        {
            if (SelectedCells.Count == 0)
                return;
            int index = SelectedCells[0].ColumnInfo.Index;

            if (MasterGridViewInfo.TableHeaderRow.Cells[index].CellElement == null)
                return;

           for (int i = 0; i < Columns.Count; i++ )
                MasterGridViewInfo.TableHeaderRow.Cells[i].CellElement.ResetValue(LightVisualElement.BackColorProperty);

            MasterGridViewInfo.TableHeaderRow.Cells[index].CellElement.SetValue(
                LightVisualElement.BackColorProperty, Color.Blue);
        }

Can you please pinpoint where is the problem?

7 Answers, 1 is accepted

Sort by
0
Nick
Telerik team
answered on 21 Aug 2009, 11:09 AM
Hello FilipN,

No visual update is started so you need to manually trigger one. Please try the following code snippet:

this.radGridView1.GridElement.Update(GridUINotifyAction.RowsChanged); 
 

If this does not work, please try the following, although this can bring substantial performance overhead:

this.radGridView1.GridElement.Update(GridUINotifyAction.Reset); 

Please write me back with the result. I am looking forward to your reply.

Regards,
Nick
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
FilipN
Top achievements
Rank 1
answered on 21 Aug 2009, 12:00 PM
Thanks for fast reply,
I tried both. It has no effect. The second one (this.radGridView1.GridElement.Update(GridUINotifyAction.Reset); actully makes things even worse, because then the column's headers don't get painted to blue at all (not even if user tries to hover over the column's headers).

Here comes the snippet i used for testing
        void CygGridViewTotalRow_SelectionChanged(object sender, EventArgs e)
        {
            if (SelectedCells.Count == 0)
                return;
            int index = SelectedCells[0].ColumnInfo.Index;

            if (MasterGridViewInfo.TableHeaderRow.Cells[index].CellElement == null)
                return;

            for (int i = 0; i < Columns.Count; i++)
                MasterGridViewInfo.TableHeaderRow.Cells[i].CellElement.ResetValue(LightVisualElement.BackColorProperty);

            MasterGridViewInfo.TableHeaderRow.Cells[index].CellElement.SetValue(
                LightVisualElement.BackColorProperty, Color.Blue);
//  First i tried this          this.GridElement.Update(GridUINotifyAction.Reset); 
//Then i commented former and uncommented this:
//        this.radGridView1.GridElement.Update(GridUINotifyAction.RowsChanged); 
0
Nick
Telerik team
answered on 24 Aug 2009, 11:27 AM
Hello FilipN,

I tried your code with our latest internal build and the header gets updated instantaneously after you set the color; you do not have to reset the grid. What values do you use for the following properties: SelectionMode and MultiSelect?

Best wishes,
Nick
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Dave Galligher
Top achievements
Rank 2
answered on 17 Mar 2011, 08:27 PM
Hi guys,

I realize this posting is fairly old, I'm on the Q32010 version but am in need of changing the background color of only certain columns. I know some of the elements described in the thread has been deprecated, so I'm using the the proper elements now, however I still don't have the results I'm looking for. The gridview is bound to a datasource, I wouldn't think this makes any difference, but I'm giving you this piece of information just in case, the column names come from a generic list that contains information about what columns I want to update based on a query process by the user, here is the code:

...
foreach (FieldItem fi in ti.Fields)                
                    gvData.MasterView.TableHeaderRow.Cells[fi.FieldName].Style.BackColor = Color.Orange;

                gvData.TableElement.Update(GridUINotifyAction.Reset);
...

fi.FieldName above contains the name of the column for the cells value (I've also tried this using the index and that doesn't work either). Examples above specified the use of .CellElement.BackColor which does not exist in my version of the code, I had to use Style.BackColor instead. The color value could be any color, I just chose Orange.

In desperation I tried the TableElement.Update command, but no positive results on this either.

Any help would be appreciated. Thank you in advance.

Dave
0
Stefan
Telerik team
answered on 21 Mar 2011, 08:20 AM
Hi Dave Galligher,

Thank you for writing.

If I understand correctly you want to style a column BackColor upon some condition. If this is the case you have to use CellFormatting (for data rows) or ViewCellFormatting (row all rows). Please refer to this help article, which demonstrates how you can customize your grid appearance using these events. 

Let me know if this works for you.

Kind regards,
Stefan
the Telerik team
0
Dave Galligher
Top achievements
Rank 2
answered on 21 Mar 2011, 05:52 PM
Using the ViewCellFormatting it does change the background color of the cell on the header, but when the control scrolls horizontally, cell colors for the column headers get messed up for columns that do not fall in the evaluated cell, here is the code:

        /// <summary>
        /// Update the color of the foreground on the filter row -- without this the color is
        /// a light gray that is hard to read on some monitors.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gvData_ViewCellFormatting(object sender, CellFormattingEventArgs e)
        {
            GridFilterCellElement filterCell = e.CellElement as GridFilterCellElement;
            if (filterCell != null)
                filterCell.FilterOperatorText.ForeColor = SystemColors.GradientActiveCaption;
            else
            {
                if (e.CellElement is GridTableHeaderCellElement || e.CellElement is GridHeaderCellElement)
                {
                    string columnText = e.CellElement.Value.ToString();
                    if (_queriedFields != null && _queriedFields.Contains(columnText))
                    {
                        e.CellElement.DrawFill = true;
                        e.CellElement.BackColor = Color.Orange;
                        e.CellElement.ForeColor = Color.White;
                        e.CellElement.GradientStyle = GradientStyles.Solid;
                    }
                }
            }
        }           

As you can see, it is looking for explicit values to change the column headers. The code executes as desired, but when the horizontal scroll happens columns that fall outside of the requirements now have the new formatting applied to them. This should not happen.

See attached images.
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 21 Mar 2011, 06:04 PM
Hello Dave,

You need to reset the values wherever the condition is not met. This is because of the RadGridView's UI Virtualization system which re-uses cells. Please have a look at this help topic on cell formatting, in particular the section at the end which is on ViewCellFormatting.

Hope that helps, but let me know if you need further information
Richard
Tags
GridView
Asked by
FilipN
Top achievements
Rank 1
Answers by
Nick
Telerik team
FilipN
Top achievements
Rank 1
Dave Galligher
Top achievements
Rank 2
Stefan
Telerik team
Richard Slade
Top achievements
Rank 2
Share this question
or