New to Telerik UI for WinForms? Download free 30-day trial

Format cell with Style property

The GridViewCellInfo.Style property gives direct access to the cell’s visual properties. It makes it possible to set styles to cells in runtime without using events like CellFormatting or the ConditionalFormattingObject.

This approach lets you customize visual properties which are defined by the theme. All changes set this way will have a greater priority than the theme.

The first thing to do for using the cell’s Style is to define what custom visual properties will use this cell. You can define that the cell will:

  • CustomizeFill

  • CustomizeBorder

Using the Style property allows you to define cell’s style properties:

  • Fill

  • Border

  • Font

  • ForeColor

The example below shows how to customize the Font and BackColor of a cell.

Font myFont = new Font(new FontFamily("Calibri"), 12.0F, FontStyle.Bold);
private void StyleCell(GridViewCellInfo cell)
{
    cell.Style.Font = myFont;
    cell.Style.CustomizeFill = true;
    cell.Style.GradientStyle = GradientStyles.Solid;
    cell.Style.BackColor = Color.FromArgb(162, 215, 255);
}

Private myFont As New Font(New FontFamily("Calibri"), 12.0F, FontStyle.Bold)
Private Sub StyleCell(cell As GridViewCellInfo)
    cell.Style.Font = myFont
    cell.Style.CustomizeFill = True
    cell.Style.GradientStyle = GradientStyles.Solid
    cell.Style.BackColor = Color.FromArgb(162, 215, 255)
End Sub
'#End Region
Private Sub FormattingCells_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'NwindDataSet.Cars' table. You can move, or remove it, as needed.
    Me.CarsTableAdapter.Fill(Me.NwindDataSet.Cars)
    RadGridView1.Columns("Picture").Width = 80
    RadGridView1.Rows(0).Height = 60
    RadGridView1.Rows(1).Height = 60
    RadGridView1.Rows(2).Height = 60
    RadGridView1.Rows(3).Height = 60
    '#region CellStyleMethodCall
    Me.StyleCell(Me.RadGridView1.Rows(1).Cells(1))

Here is how to call this method of a certain cell:

this.StyleCell(this.radGridView1.Rows[1].Cells[1]);

Me.StyleCell(Me.RadGridView1.Rows(1).Cells(1))

Figure 1: Format using the Style property.

WinForms RadGridView Format Style Property

Before assigning a certain value to the Style of the data cell, you can store the initial values of the properties. Thus, you can reset the style to the initial values if the style is not needed anymore.

See Also

In this article