Category / Section
How to set the back color for the GroupCaptionPlusMinusCell in the WinForms GridGroupingControl?
2 mins read
Backcolor of GroupCaptioncell
By default, the back color of the GroupCaptionPlusMinusCell cannot be set through the cell style properties, as it is a button cell. To set the back color of the GroupCaptionPlusMinusCell, you can customize the TableControlDrawCell event. The following code explains how to set the back color to the cell. In the provided example, the back color is applied through the FillRectangle() method and the indicators (+/-) are written by using the DrawString() method.
C#
this.gridGroupingControl1.TableControlDrawCell += gridGroupingControl1_TableControlDrawCell; void gridGroupingControl1_TableControlDrawCell(object sender, GridTableControlDrawCellEventArgs e) { GridTableCellStyleInfo style = e.Inner.Style as GridTableCellStyleInfo; //Sets the backcolor to the GroupCaptionPlusMinusCell. if(style.TableCellIdentity.TableCellType == GridTableCellType.GroupCaptionPlusMinusCell) { e.Inner.Cancel = true; e.Inner.Graphics.FillRectangle(new SolidBrush(Color.Red), e.Inner.Bounds); e.Inner.Graphics.DrawRectangle(Pens.Black, e.Inner.Bounds); Element el = style.TableCellIdentity.DisplayElement; string text = el.ParentGroup.IsExpanded ? "-" : "+"; Font font = new Font(e.Inner.Style.GdipFont.Name, 10f, FontStyle.Bold); int xSpacing = 2, ySpacing = 2; if(text == "-") { xSpacing = 4; ySpacing = 1; } //The plus ad minus(+/-) are printed at the exact position in the cell. e.Inner.Graphics.DrawString(text, font, new SolidBrush(Color.White), new Point(e.Inner.Bounds.X + xSpacing, e.Inner.Bounds.Y + ySpacing)); } }
VB
Private Me.gridGroupingControl1.TableControlDrawCell += AddressOf gridGroupingControl1_TableControlDrawCell Private Sub gridGroupingControl1_TableControlDrawCell(ByVal sender As Object, ByVal e As GridTableControlDrawCellEventArgs) Dim style As GridTableCellStyleInfo = TryCast(e.Inner.Style, GridTableCellStyleInfo) 'Sets the backcolor to the GroupCaptionPlusMinusCell. If style.TableCellIdentity.TableCellType = GridTableCellType.GroupCaptionPlusMinusCell Then e.Inner.Cancel = True e.Inner.Graphics.FillRectangle(New SolidBrush(Color.Red), e.Inner.Bounds) e.Inner.Graphics.DrawRectangle(Pens.Black, e.Inner.Bounds) Dim el As Element = style.TableCellIdentity.DisplayElement Dim text As String = If(el.ParentGroup.IsExpanded, "-", "+") Dim font As New Font(e.Inner.Style.GdipFont.Name, 10f, FontStyle.Bold) Dim xSpacing As Integer = 2, ySpacing As Integer = 2 If text = "-" Then xSpacing = 4 ySpacing = 1 End If 'The plus ad minus(+/-) are printed at the exact position in the cell. e.Inner.Graphics.DrawString(text, font, New SolidBrush(Color.White), New Point(e.Inner.Bounds.X + xSpacing, e.Inner.Bounds.Y + ySpacing)) End If End Sub
The following screenshot displays the Grid after the properties are applied.
Figure 1: Back color applied for GroupCaptionPlusMinusCell