Change the background color of a cell when it is a child column of a Stacked header

I have tried to trap for the header columns for header cells that are within a stacked header. I need to be able to get the display text of the stacked header or the column name of the header cell to determine if the color should be changed. 
I can trap for the Stacked header and can change the background color of it, I just can not figure out how to change the child headers with the stacked header.

here is the code that I use in the DrawCell method to change the Stacked Header:

  If (TryCast(e.DataRow, DataRowBase)).RowType = RowType.StackedHeaderRow And e.CellValue IsNot Nothing Then
            If ColorColumns.Contains(e.CellValue) Then
                e.Style.BackColor = MonthColumnBackgroundColor
            End If

        End If


5 Replies 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team January 25, 2021 02:54 PM UTC

Hi George Busby,

Thank you for contacting Syncfusion Support.

We are little unclear with your scenario. Can you please provide the more information related to your query?

Can you please share us below things?
       
        1. Brief replication procedure/video illustration of the reported issue
        2.
Could you please provide more details about your scenario with Image illustrations?
        3. Provide code snippet related to customization in Staked header
        4. Provide code snippet related to customization in SfDataGrid    
 
It will be helpful for us to check on it and provide you the solution at the earliest.

Regards,
Vijayarasan S
 



GB George Busby January 26, 2021 12:22 AM UTC

Here is a snap shot of the screen:


I want the "Hrs" and "%" columns that are under the yellow tinted stacked headers to have the same yellow tint. So in the example above the "Hrs" and "%" column within the "2021 Feb" stacked header for the detail records should be the yellow tint that the stacked header has. I use the code in the previous message to set the Stacked Header for every other group to have the Yellow tint. I was hoping the in the same DrawCell method I could also change the column headers for the "Hrs" and "%" column that are associated with the Stacked Header to have the same background color.


VS Vijayarasan Sivanandham Syncfusion Team January 26, 2021 04:17 PM UTC

Hi George Busby,

Thanks for the update.

Based on provided information your requirement of apply the Column header style based on Stacked Header Column contains child column in Details View DataGrid. Your requirement can be achieved by customization the Draw cell event in ChildDataGrid. Please refer the below code snippet for your reference,

 
//trigger the Draw cell event for DetailsView DataGrid 
childGrid.DrawCell += SfDataGrid1_DrawCell; 
 
 
private void SfDataGrid1_DrawCell(object sender, Syncfusion.WinForms.DataGrid.Events.DrawCellEventArgs e) 
{            
            //Get the Stakced Header Row in Details View DataGrid 
            if ((e.DataRow as DataRowBase).RowType == Syncfusion.WinForms.DataGrid.Enums.RowType.StackedHeaderRow) 
            { 
                int columnIndex = e.ColumnIndex; 
 
                //get the Stakced Header Column  
                if (e.CellValue == "Sales Details") 
                { 
                    //Apply style  to Stacked Header 
                    e.Style.BackColor = Color.Yellow;          
 
                    //check the index for avoid the Index Out range exception 
                    if (childGrid.StackedHeaderRows[e.RowIndex].StackedColumns.Count == e.ColumnIndex) 
                        columnIndex = e.ColumnIndex - 1; 
 
                    //get the Child Column of specific Stacked header column 
                    var childColumnName = childGrid.StackedHeaderRows[e.RowIndex].StackedColumns[columnIndex].ChildColumns.Split(',').ToList<string>(); 
 
                    foreach (var stackedColumnName in childColumnName.ToList()) 
                    { 
                        //apply the Column Header Style based on Stacked Header child Columns 
                        childGrid.Columns[stackedColumnName].HeaderStyle.BackColor = Color.Yellow; 
                         
                    } 
                } 
          
                if (e.CellValue.ToString() == "Order Details") 
                { 
                    //Apply style  to Stacked Header 
                    e.Style.BackColor = Color.DarkCyan; 
                    e.Style.TextColor = Color.White; 
 
                    if (childGrid.StackedHeaderRows[e.RowIndex].StackedColumns.Count == e.ColumnIndex) 
                        columnIndex = e.ColumnIndex - 1; 
 
                    var childColumnName = childGrid.StackedHeaderRows[e.RowIndex].StackedColumns[columnIndex].ChildColumns.Split(',').ToList<string>(); 
                     
                    foreach (var stackedColumnName in childColumnName.ToList()) 
                    { 
                        //apply the Column Header Style based on Stacked Header child Columns 
                        childGrid.Columns[stackedColumnName].HeaderStyle.BackColor = Color.DarkCyan;                         
                        childGrid.Columns[stackedColumnName].HeaderStyle.TextColor = Color.White; 
                    } 
                } 
                if (e.CellValue == "Customer Details") 
                { 
                    e.Style.BackColor = Color.LightCyan; 
 
                    if (childGrid.StackedHeaderRows[e.RowIndex].StackedColumns.Count == e.ColumnIndex) 
                        columnIndex = e.ColumnIndex - 1; 
 
                    var childColumnName = childGrid.StackedHeaderRows[e.RowIndex].StackedColumns[columnIndex].ChildColumns.Split(',').ToList<string>(); 
                     
                    foreach (var stackedColumnName in childColumnName.ToList()) 
                    { 
                        //apply the Column Header Style based on Stacked Header child Columns 
                        childGrid.Columns[stackedColumnName].HeaderStyle.BackColor = Color.LightCyan;                         
                    } 
                } 
                if (e.CellValue == "Product Details") 
                { 
                    e.Style.BackColor = Color.DarkGray; 
                    e.Style.TextColor = Color.White; 
 
                    if (childGrid.StackedHeaderRows[e.RowIndex].StackedColumns.Count == e.ColumnIndex) 
                        columnIndex = e.ColumnIndex - 1; 
                    var childColumnName = childGrid.StackedHeaderRows[e.RowIndex].StackedColumns[columnIndex].ChildColumns.Split(',').ToList<string>(); 
                     
                    foreach (var stackedColumnName in childColumnName.ToList()) 
                    { 
                        //apply the Column Header Style based on Stacked Header child Columns 
                        childGrid.Columns[stackedColumnName].HeaderStyle.BackColor = Color.DarkGray; 
                        childGrid.Columns[stackedColumnName].HeaderStyle.TextColor = Color.White; 
                    } 
                } 
            } 
} 

Screenshot for your reference,
 
Please let us know, if you require further assistance on this. 
Regards,
Vijayarasan S




Marked as answer

GB George Busby January 26, 2021 10:15 PM UTC

Thank You for the code sample. I had to make a few updates to get it to work in my code, but it is highlighting the header cells the way I want them to.


VS Vijayarasan Sivanandham Syncfusion Team January 27, 2021 12:31 PM UTC

Hi George Busby, 
 
Thanks for the update. 
 
We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you😊. 
 
Regards, 
Vijayarasan S 


Loader.
Up arrow icon