# Formatting cells

**RadScheduler** uses a logical layer for storing appointments and view data, and the visual elements that represent this data are occasionally being refreshed. Thus, you might lose some of the properties you have set to the visual elements. In order to customize the properties of the cell elements, you should use the **CellFormatting** event.

This event is fired after a **SchedulerCellElement** has been created, and when the cell needs to be displayed. The arguments of this event contain a reference to the cell element itself, so you can modify its properties. The samples below demonstrate how you can programmatically change various properties of the **RadScheduler** cells. Before these samples, we are going to provide some useful information about the **RadScheduler** cells types.

> caption Figure 1: Visual Elements

![WinForms RadScheduler Visual Elements](https://www.telerik.com/products/winforms/documentation/assets/0f7dc2664da0a719d029d82d029c3c56/scheduler-views-formatting-cells001.png)

## RadScheduler Data Cells

**RadScheduler** data cells are of type **SchedulerCellElement**. This type of cell provides several useful properties:

* **Date**: Returns the date of the cell.

* **IsToday**: Indicates whether the date of the cells is today.

* **IsWorkTime**: Gets or sets whether the cell represents a work time slot.

* **IsWholeHour**: Indicates whether the cell represents a whole hour.

## RadScheduler Header Cells

**RadScheduler** header cells are of type **SchedulerHeaderCellElement**. This type derive from **SchedulerCellElement**, so the properties mentioned above are valid here as well. **SchedulerHeaderCellElement** exposes one more property  that you may find useful:

* **IsVertical**: Returns whether the cell element is vertical or not.

The interesting fact about **SchedulerHeaderCellElement** is that cells of this type are used for two different operations. For example, let's get the DayView. You can see that there is a header cell with text for each of the days. The cell below, which allows for hosting all-day appointments, is of type SchedulerHeaderCellElement as well. If you need to programmatically differentiate the first cell from the second, you can do this by the **Parent** property of the cell. The header cells that contain text are contained by an element of type **DayViewHeader**. The cells that can contain all-day appointments are contained by an element of type **DayViewAllDayHeader**.

> tip Still wondering what type of cell is the cell that you want to format? You can employ[ RadControlSpy](https://www.telerik.com/products/winforms/documentation/tools/controlspy/controlspy) for this task.

## Formatting Data Cells

Let's now format some data cells. We will make red the SchedulerCellElements that represent a specific day, for example Apr 13. Let's note that SchedulerHeaderCellElement derive from SchedulerDataElement, so we need to include an 'if' clause that prevents the header cells from being painted:

#### Data Cells

```C#
void radScheduler1_CellFormatting1(object sender, Telerik.WinControls.UI.SchedulerCellEventArgs e)
{
    if (e.CellElement.Date.Month == 4 && e.CellElement.Date.Day == 13)
    {
        if (!(e.CellElement is SchedulerHeaderCellElement))
        {
            e.CellElement.BackColor = Color.Red;
        }                
    }
}
```

```VB.NET
Private Sub radScheduler1_CellFormatting1(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.SchedulerCellEventArgs)
    If e.CellElement.Date.Month = 4 AndAlso e.CellElement.Date.Day = 13 Then
        If Not (TypeOf e.CellElement Is SchedulerHeaderCellElement) Then
            e.CellElement.BackColor = Color.Red
        End If
    End If
End Sub
```

We have the desired result. The data cells of April 13 are red.

> caption Figure 2: Formatted Cells

![WinForms RadScheduler Formatted Cells](https://www.telerik.com/products/winforms/documentation/assets/2edd5807ebdd12dbbed8f1e99ee0b512/scheduler-views-formatting-cells002.png)

However, what will happen if we navigate left or right in order to see the next/previous days of the  days that are currently into view? Here is the result.

> caption Figure 3: Formatting Incorrect Result

![WinForms RadScheduler Formatting Incorrect Result](https://www.telerik.com/products/winforms/documentation/assets/6e0d8dd39ee86e401ac2d4fe926c4586/scheduler-views-formatting-cells003.png)

As you can see, undesired cells become red as well. RadScheduler is using elements recycling which means that the elements are reused and this is why other cells appear red also. To avoid this you should add an 'else' clause that will reset the BackColor property:

#### Reset Loaclly Set Values

```C#
void radScheduler1_CellFormatting2(object sender, Telerik.WinControls.UI.SchedulerCellEventArgs e)
{
    if (e.CellElement.Date.Month == 4 && e.CellElement.Date.Day == 13)
    {
        if (!(e.CellElement is SchedulerHeaderCellElement))
        {
            e.CellElement.BackColor = Color.Red;
        }    
    }
    else
    {
        if (!(e.CellElement is SchedulerHeaderCellElement))
        {
            e.CellElement.ResetValue(VisualElement.BackColorProperty, ValueResetFlags.Local);
        }
    }
}
```

```VB.NET
Private Sub radScheduler1_CellFormatting2(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.SchedulerCellEventArgs)
    If e.CellElement.Date.Month = 4 AndAlso e.CellElement.Date.Day = 13 Then
        If Not (TypeOf e.CellElement Is SchedulerHeaderCellElement) Then
            e.CellElement.BackColor = Color.Red
        End If
    Else
        If Not (TypeOf e.CellElement Is SchedulerHeaderCellElement) Then
            e.CellElement.ResetValue(VisualElement.BackColorProperty, ValueResetFlags.Local)
        End If
    End If
End Sub
```

As you can see in the screenshot below, the styling is now correct, because if the red cells now represent a day different from April 13, the red color is removed.

> caption Figure 4: Formatting Correct Result

![WinForms RadScheduler Formatting Correct Result](https://www.telerik.com/products/winforms/documentation/assets/6bc8455625d9e1206df76612583fe5ff/scheduler-views-formatting-cells004.png)

## Formatting Header Cells.

We are going to make the header cells that displays the text orange, while the header cell that contains all-day appointment will become dark red:

#### Header Cells.

```C#
void radScheduler1_CellFormatting3(object sender, Telerik.WinControls.UI.SchedulerCellEventArgs e)
{
    if (e.CellElement is SchedulerHeaderCellElement)
    {
        if (e.CellElement.Date.Month == DateTime.Now.Month && e.CellElement.Date.Day == DateTime.Now.Day)
        {
            if (e.CellElement.Parent is DayViewHeader)
            {
                e.CellElement.Text = "Birthday!";
                e.CellElement.BackColor = Color.Yellow;
                e.CellElement.GradientStyle = GradientStyles.Solid;
                e.CellElement.DrawFill = true;
            }
            else if (e.CellElement.Parent is DayViewAllDayHeader && e.CellElement.Text != "Local")
            {
                e.CellElement.DrawFill = true;
                e.CellElement.BackColor = Color.DarkRed;
            }
            else
            {
                e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
                e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
                e.CellElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
            }
        }
        else
        {
            e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
            e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
            e.CellElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
        }
    }
}
```

```VB.NET
Private Sub radScheduler1_CellFormatting3(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.SchedulerCellEventArgs)
    If TypeOf e.CellElement Is SchedulerHeaderCellElement Then
        If e.CellElement.[Date].Month = DateTime.Now.Month AndAlso e.CellElement.[Date].Day = DateTime.Now.Day Then
            If TypeOf e.CellElement.Parent Is DayViewHeader Then
                e.CellElement.Text = "Birthday!"
                e.CellElement.BackColor = Color.Yellow
                e.CellElement.GradientStyle = GradientStyles.Solid
                e.CellElement.DrawFill = True
            ElseIf TypeOf e.CellElement.Parent Is DayViewAllDayHeader AndAlso e.CellElement.Text <> "Local" Then
                e.CellElement.DrawFill = True
                e.CellElement.BackColor = Color.DarkRed
            Else
                e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local)
                e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local)
                e.CellElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local)
            End If
        Else
            e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local)
            e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local)
            e.CellElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local)
        End If
    End If
End Sub
```

The result of the code snippet above is shown below:

> caption Figure 5: Formatted Header Cells

![WinForms RadScheduler Formatted Header Cells](https://www.telerik.com/products/winforms/documentation/assets/0dd706b81bf9b5212bf5d14e95c1e24c/scheduler-views-formatting-cells005.png)

## See Also

* [Visual Style Builder](https://www.telerik.com/products/winforms/documentation/styling-and-appearance/visual-style-builder)
* [Using Default Themes](https://www.telerik.com/products/winforms/documentation/styling-and-appearance/using-default-themes)
* [Views](https://www.telerik.com/products/winforms/documentation/controls/scheduler/views/overview-and-structure)
* [Working with Appointments](https://www.telerik.com/products/winforms/documentation/controls/scheduler/appointments-and-dialogs/working-with-appointments)
