Articles in this section
Category / Section

How to apply column header style based on StackedHeadercolumn style in WinForms DetailsViewDataGrid (SfDataGrid)?

2 mins read

WinForms DataGrid (SfDataGrid) does not provide the direct support to apply column header style based on StackedHeaderColumn style. You can apply column header based on StackedHeaderColumn style by customization the DrawCell event in WinForms DataGrid (SfDataGrid).

//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;
                    }
                }
            }
}

Shows the column header style applied in SfDataGridThe following screenshot shows the column header style based on StackedHeaderColumn style,Shows the column header style based on StackedHeadercolumn style in SfDataGridTake a moment to peruse the WinForms DataGrid – Stacked Headers documentation, where you can find about stacked headers with code examples.

You can download the example from GitHub

Conclusion

I hope you enjoyed learning about how to apply column header style based on stackedHeadercolumn style in WinForms Datagrid.

You can refer to our WinForms DataGrid feature tour page to know about its other groundbreaking feature representations documentation

and how to quickly get started for configuration specifications.  You can also explore our WinForms DataGrid example
to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied