Articles in this section
Category / Section

How to set the header cell color for the WinForms GridControl?

3 mins read

Applied header cell color


You can change the color of the particular header cell in a WinForms GridControl 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

//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;
      }
   }
}
'Hook the event to change the back color for the header cells.
AddHandler 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 the ChangeCells method

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);
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.

Applied header color in WinForms Grid


    Figure
1: Applied Header Color

Conclusion

I hope you enjoyed learning about how to set the header cell color for the WinForms GridControl.

You can refer to WinForms GridControl feature tour page to know about its other groundbreaking feature representations and WinForms GridControl 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!

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