How to set the header cell color for the WinForms GridControl?
Applied header cell color
You can change the color of the particular header cell in a WinForms Grid control by setting its BackColor property. You can achieve this by using the PrepareViewStyleInfo event handler. And also by using the ChangeCells() method, you can change the BackColor property for the particular range of cells.
Using PrepareViewStyleInfo Event
C#
//Hook the event to change the back color for the header cells. this.gridControl1.PrepareViewStyleInfo += gridControl1_PrepareViewStyleInfo; void gridControl1_PrepareViewStyleInfo(object sender, GridPrepareViewStyleInfoEventArgs e) { if (e.RowIndex == 0) { if (e.ColIndex == 1) { e.Style.BackColor = Color.LightBlue; } if (e.ColIndex == 2) { e.Style.BackColor = Color.LightGreen; } if (e.ColIndex == 3) { e.Style.BackColor = Color.LightPink; } if (e.ColIndex == 4) { e.Style.BackColor = Color.LightGray; } } }
VB
'Hook the event to change the back color for the header cells. Private Me.gridControl1.PrepareViewStyleInfo += AddressOf gridControl1_PrepareViewStyleInfo Private Sub gridControl1_PrepareViewStyleInfo(ByVal sender As Object, ByVal e As GridPrepareViewStyleInfoEventArgs) If e.RowIndex = 0 Then If e.ColIndex = 1 Then e.Style.BackColor = Color.LightBlue End If If e.ColIndex = 2 Then e.Style.BackColor = Color.LightGreen End If If e.ColIndex = 3 Then e.Style.BackColor = Color.LightPink End If If e.ColIndex = 4 Then e.Style.BackColor = Color.LightGray End If End If End Sub
Using ChangeCells() method
C#
GridStyleInfo style = this.gridControl1[0, 0]; style.BackColor = Color.Pink; //Change the Header color usign style object this.gridControl1.ChangeCells(GridRangeInfo.Cells(0, 5, 0, 8), style);
VB
Dim style As GridStyleInfo = Me.gridControl1(0, 0) style.BackColor = Color.Pink 'Change the Header color usign style object Me.gridControl1.ChangeCells(GridRangeInfo.Cells(0, 5, 0, 8), style)
The following screenshot illustrates the output.
Figure 1: Applied Header Color
Samples:
C#: SetHeaderColorForPerticularCell
VB: SetHeaderColorForPerticularCell
Conclusion
I hope you enjoyed learning about how to set the header cell color for the WinForms GridControl.
You can refer to our WinForms GridControl feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.
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 forums, Direct-Trac, or feedback portal. We are always happy to assist you!