Highlight Header Background With Selection

I was wondering if there was a way to change the background color of the header with the selection of cells. So if I was to select cells in the range of C2:D4 the column headers C and D and the row headers 2, 3 and 4 would change the background color as well.

I have tried this on my own and I can get the row headers to change the background color, but the column headers are not changing with it.

Here is how I am originally setting the header colors:
grid.Model.HeaderStyle.Background = new SolidColorBrush(System.Windows.Media.Colors.WhiteSmoke);

Here is how I am setting my cell selection changed:
grid.SelectionChanged += new GridSelectionChangedEventHandler(grid_SelectionChanged);

Here is that function:
void grid_SelectionChanged(object sender, GridSelectionChangedEventArgs e)
        {
            List<SelectedCell> cells = new List<SelectedCell>();

            for(int i = 0; i < grid.Model.RowCount; ++i)
            {
                grid.Model[i, 0].Background = new SolidColorBrush(System.Windows.Media.Colors.WhiteSmoke);
            }

            for(int i = 0; i < grid_blueprint.Model.RowCount; ++i)
            {
                grid.Model[0, i].Background = new SolidColorBrush(System.Windows.Media.Colors.WhiteSmoke);
            }

            foreach(GridRangeInfo range1 in grid.Model.Selections.Ranges)
            {
                int row, col;
                int trackRow = -1;
                GridRangeInfo range = range1.ExpandRange(0, 0, grid.Model.RowCount, grid.Model.ColumnCount);
                rangeInfo2 = range;                  

                if (range.GetFirstCell(out row, out col))
                {           
                    cells.Add(new SelectedCell() { Row = row, Column = col });
                    trackRow = row;
                    grid.Model[row, 0].Background = new SolidColorBrush(System.Windows.Media.Colors.DarkOrange);
                    grid.Model[0, col].Background = new SolidColorBrush(System.Windows.Media.Colors.DarkOrange);

                    while (range.GetNextCell(ref row, ref col))
                    {
                        if(row != trackRow)
                            trackRow = row;

                        grid.Model[row, 0].Background = new SolidColorBrush(System.Windows.Media.Colors.DarkOrange);
                        grid.Model[1, col].Background = new SolidColorBrush(System.Windows.Media.Colors.DarkOrange);
                        cells.Add(new SelectedCell() { Row = row, Column = col });
                    }
                }
            }
            
            CellSelection = cells;
        }

Thanks for any help.


3 Replies

PS Pannir Selvam S Syncfusion Team January 16, 2017 07:25 AM UTC

Hi David, 

Thanks for contacting Syncfusion support. 

You can achieve you requirement “Highlighting the corresponding Header cells based on the selected cells ” by handling the QueryCellInfo and SelectionChanged event of GridModel like below code example. 

Code Snippet[c#]: 
grid.Model.QueryCellInfo += Model_QueryCellInfo; 
grid.Model.SelectionChanged += Model_SelectionChanged; 
 
private void Model_SelectionChanged(object sender, GridSelectionChangedEventArgs e) 
{ 
    //Invalidate the corresponding Header rows and colums when selection is changed. 
    grid.Model.InvalidateCell(GridRangeInfo.Cols(0, grid.Model.HeaderColumns - 1)); 
    grid.Model.InvalidateCell(GridRangeInfo.Rows(0, grid.Model.HeaderRows - 1)); 
    grid.Model.InvalidateVisual(); 
} 
 
private void Model_QueryCellInfo(object sender, GridQueryCellInfoEventArgs e) 
{ 
    //Modify the background of header cells based on cell selection. 
    if ((e.Cell.RowIndex == grid.Model.HeaderRows - 1 && grid.Model.SelectedRanges.AnyRangeIntersects(GridRangeInfo.Col(e.Cell.ColumnIndex))) 
        || (e.Cell.ColumnIndex == grid.Model.HeaderColumns - 1 && grid.Model.SelectedRanges.AnyRangeIntersects(GridRangeInfo.Row(e.Cell.RowIndex)))) 
    { 
        e.Style.Background = new SolidColorBrush(Colors.DarkOrange); 
    } 
 
    if (e.Cell.RowIndex == 0 && e.Cell.ColumnIndex == 0) 
        return; 
    if (e.Cell.RowIndex == 0) 
        e.Style.CellValue = GridRangeInfo.GetAlphaLabel(e.Cell.ColumnIndex); 
    else if(e.Cell.ColumnIndex == 0) 
        e.Style.CellValue = e.Cell.RowIndex; 
} 

You can download the sample for this from below location. 


Please let us know if you have any queries. 

Pannir 



DA David January 17, 2017 08:26 PM UTC

Hi Pannir,

Thanks for your example, that worked great for me!


KB Kanimozhi Bharathi Syncfusion Team January 18, 2017 08:58 AM UTC

Hi David, 
Thank you for your update. 
Regards 
Kanimozhi B 


Loader.
Up arrow icon