We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Highlight Header Background With Selection

Thread ID:

Created:

Updated:

Platform:

Replies:

128286 Jan 12,2017 01:57 PM UTC Jan 18,2017 08:58 AM UTC WPF 3
loading
Tags: GridControl
David
Asked On January 12, 2017 01:57 PM UTC

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.


Pannir Selvam S [Syncfusion]
Replied On 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 


David
Replied On January 17, 2017 08:26 PM UTC

Hi Pannir,

Thanks for your example, that worked great for me!

Kanimozhi Bharathi [Syncfusion]
Replied On January 18, 2017 08:58 AM UTC

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


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;