How to keep the same background style in WinForms GridGroupingControl?
Header style
When you set
the cell type of the column header a Checkbox, the header style changes.
Solution:
The GetGridstyleInfo method has to be used to keep the same background color of the header cell while changing the header cell type. The required customization is done in this GetGridStylenfo method. You have to use the QuerycellstyleInfo event to get the style and then pass this style to the GetGridstyleInfo method.
// QueryCellStyleInfo event.
void gridGroupingControl1_QueryCellStyleInfo(object sender, GridTableCellStyleInfoEventArgs e)
{
if(e.TableCellIdentity.TableCellType == GridTableCellType.ColumnHeaderCell && e.TableCellIdentity.Column.Name == "Property1")
{
// set the celltype as checkbox in column 1.
e.Style.CellType = GridCellTypeName.CheckBox;
e.Style.Description = e.Style.Text;
e.Style.CellValueType = typeof(bool);
e.Style.CellValue = true;
// to apply the style of grid.
e.Style.Interior = GetThemeInterior(e.Style);
}
}
// to fill the grid background color(GetGridstyleInfo).
private BrushInfo GetThemeInterior(GridStyleInfo style)
{
// apply the color for theme.
GridVisualStyles visualStyles = this.gridGroupingControl1.TableModel.Options.GridVisualStyles;
switch (visualStyles)
{
case GridVisualStyles.Office2007Blue:
return new BrushInfo(GradientStyle.Vertical, Color.FromArgb(249, 252, 255), Color.FromArgb(197, 222, 255));
case GridVisualStyles.Office2010Silver:
return new BrushInfo(GradientStyle.Vertical, Color.FromArgb(223, 227, 232), Color.FromArgb(183, 188, 193));
default:
style.TextColor = Color.Black;
return new BrushInfo(GradientStyle.Vertical, Color.FromArgb(249, 252, 255), Color.FromArgb(197, 222, 255));
}
}'QueryCellStyleInfo event
Private Sub gridGroupingControl1_QueryCellStyleInfo(ByVal sender As Object, ByVal e As GridTableCellStyleInfoEventArgs)
If e.TableCellIdentity.TableCellType = GridTableCellType.ColumnHeaderCell AndAlso e.TableCellIdentity.Column.Name = "Property1" Then
'Set the cell type as checkbox in column 1
e.Style.CellType = GridCellTypeName.CheckBox
e.Style.Description = e.Style.Text
e.Style.CellValueType = GetType(Boolean)
e.Style.CellValue = True
'Apply the style of grid
e.Style.Interior = GetThemeInterior(e.Style)
End If
End Sub
'To fill the grid background color (GetGridStyleInfo)
Private Function GetThemeInterior(ByVal style As GridStyleInfo) As BrushInfo
'Apply the color for theme
Dim visualStyles As GridVisualStyles = Me.gridGroupingControl1.TableModel.Options.GridVisualStyles
Select Case visualStyles
Case GridVisualStyles.Office2007Blue
Return New BrushInfo(GradientStyle.Vertical, Color.FromArgb(249, 252, 255), Color.FromArgb(197, 222, 255))
Case GridVisualStyles.Office2010Silver
Return New BrushInfo(GradientStyle.Vertical, Color.FromArgb(223, 227, 232), Color.FromArgb(183, 188, 193))
Case Else
style.TextColor = Color.Black
Return New BrushInfo(GradientStyle.Vertical, Color.FromArgb(249, 252, 255), Color.FromArgb(197, 222, 255))
End Select
End FunctionNote:
To learn how to use the checkbox and other functions, refer to the following link:
https://www.syncfusion.com/kb/3954
Samples:
C#: Header style
VB: Header style