# Setting CellTemplate and CellEditTemplate

This article describes how to customize the RadGridView columns by using predefined column types and how to extend the functionality of the columns by defining a non-default editor.

## Using Predefined Column Types

RadGridView provides a set of predefined column types like **GridViewCheckBoxColumn**, **GridViewComboBoxColumn**, **GridViewSelectColumn**, etc. that you can customize in accordance with the requirements and the type of properties exposed in your business object.

When you use a standard **GridViewDataColumn**, the RadGridView control automatically generates controls for the cell's view and edit modes:

* Default controls in view mode:

  * **TextBlock**: For **string**, **numeric** and **DateTime** properties

  * **CheckBox**: For **boolean** properties

* Default controls in edit mode:

  * **TextBox**: For **string** and **numeric** properties

  * **CheckBox**: For **boolean** properties

  * **RadDatePicker**: For **DateTime** properties

## Creating CellTemplate and CellEditTemplate for a Column

Sometimes you need to extend the functionality of a column and define a custom editor. To achieve this, create a **CellTemplate** or **CellEditTemplate** for the column:

* **CellTemplate**: Gets or sets the data template for the cell in view mode.

* **CellEditTemplate**: Gets or sets the data template for the cell in edit mode.

> tip You can also conditionally apply different data templates to RadGridView cells by using the [CellTemplateSelector and CellEditTemplateSelector](https://www.telerik.com/products/wpf/documentation/controls/radgridview/template-selectors/cell-template-selector) properties.

**Example 1** demonstrates how to use both properties to customize your columns. We set the **RadDateTimePicker** as a **CellEditTemplate** for the **DateTime** column type and the **RadNumericUpDown** as a **CellTemplate** for the **Salary** column.

**Example 1: Setting columns' CellTemplate and CellEditTemplate**

```XAML
<telerik:RadGridView x:Name="EmployeesGrid" AutoGenerateColumns="False" ItemsSource="{Binding Employees}">
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn DataMemberBinding="{Binding FirstName}" />
        <telerik:GridViewDataColumn DataMemberBinding="{Binding LastName}" />
        <telerik:GridViewDataColumn DataMemberBinding="{Binding StartingDate}">
            <telerik:GridViewDataColumn.CellEditTemplate>
                <DataTemplate>
                    <telerik:RadDateTimePicker SelectedDate="{Binding StartingDate}" IsTooltipEnabled="False"/>
                </DataTemplate>
            </telerik:GridViewDataColumn.CellEditTemplate>
        </telerik:GridViewDataColumn>
        <telerik:GridViewDataColumn DataMemberBinding="{Binding Salary}">
            <telerik:GridViewDataColumn.CellTemplate>
                <DataTemplate>
                    <telerik:RadNumericUpDown Value="{Binding Salary}" />
                </DataTemplate>
            </telerik:GridViewDataColumn.CellTemplate>
        </telerik:GridViewDataColumn>
    </telerik:RadGridView.Columns>
</telerik:RadGridView>
```

The RadGridView defined above will be displayed as shown in **Figure 1**.

> caption RadGridView with CellTemplate and CellEditTemplate defined

![Telerik UI for WPF RadGridView using CellTemplate and CellEditTemplate for date and salary columns](https://www.telerik.com/products/wpf/documentation/assets/bc1cebbea187dd3026175dec621d166f/radgridview_celltemplate_celledittemplate.png)

## Making Buttons Accessible

You do not need to set a RadGridView property for a button defined in a `CellTemplate` to be exposed to UI Automation. RadGridView exposes the cell content as a child automation element. Make sure that the global [AutomationMode](https://www.telerik.com/products/wpf/documentation/common-information/common-ui-automation) setting is not `Disabled`.

The button owns its accessible name. Set the `AutomationProperties.Name` property when the button content does not provide a meaningful name, such as when the button displays an icon or a custom visual.

**Example 2: Set the accessible name of a button in a CellTemplate**

```XAML
	<telerik:GridViewDataColumn Header="Actions">
		<telerik:GridViewDataColumn.CellTemplate>
			<DataTemplate>
				<Button Content="Open"
				        AutomationProperties.Name="Open item" />
			</DataTemplate>
		</telerik:GridViewDataColumn.CellTemplate>
	</telerik:GridViewDataColumn>
```

Use an accessibility testing tool to verify that the button is exposed with the expected name and control type in your application.

> important When you define a **CellEditTemplate** for a column, the **default validation** mechanism is **bypassed** as you're directly binding to the source property. If you require this validation, [create a custom column editor](https://www.telerik.com/products/wpf/documentation/controls/radgridview/managing-data/how-to/howto-create-custom-editor) instead.

The first example illustrates how to define either a **CellTemplate** or **CellEditTemplate**. In the next example, we demonstrate how to set them both for the same column.

**Example 3: Setting the CellTemplate and CellEditTemplate of a single column**

```XAML
<telerik:RadGridView x:Name="EmployeesGrid" AutoGenerateColumns="False" ItemsSource="{Binding Employees}">
	<telerik:RadGridView.Columns>
		<telerik:GridViewDataColumn DataMemberBinding="{Binding FirstName}" />
		<telerik:GridViewDataColumn DataMemberBinding="{Binding LastName}" />
		<telerik:GridViewDataColumn DataMemberBinding="{Binding StartingDate}" />
		<telerik:GridViewDataColumn DataMemberBinding="{Binding Salary}">
			<telerik:GridViewDataColumn.CellTemplate>
				<DataTemplate>
					<StackPanel Orientation="Horizontal">
						<TextBlock Text="$" />
						<TextBlock Text="{Binding Salary}" Foreground="Green" />
					</StackPanel>
				</DataTemplate>
			</telerik:GridViewDataColumn.CellTemplate>
			<telerik:GridViewDataColumn.CellEditTemplate>
				<DataTemplate>
					<telerik:RadNumericUpDown Value="{Binding Salary}" />
				</DataTemplate>
			</telerik:GridViewDataColumn.CellEditTemplate>
		</telerik:GridViewDataColumn>
	</telerik:RadGridView.Columns>
</telerik:RadGridView>
```

The RadGridView defined above will be displayed as shown in **Figure 1**.

> caption RadGridView with CellTemplate and CellEditTemplate defined for a single column

![Telerik UI for WPF RadGridView using both CellTemplate and CellEditTemplate on a single salary column](https://www.telerik.com/products/wpf/documentation/assets/89bd8679f547d97d478c5b89854bff15/radgridview_celltemplate_celledittemplate2.png)

## Tips about CellTemplate and CellEditTemplate

* You can set the **CellTemplate** and **CellEditTemplate** in the code behind. See the [Using DataTemplate in Code](https://www.telerik.com/products/wpf/documentation/common-information/using-datatemplates-in-code) article for details.

* If you set DataMemberBinding set to StartingDate, the aggregation, sorting, grouping and filtering operations on the column will be done based on the StartingDate property. If the user does not need to perform any aggregation, sorting, grouping or filtering operations for the column, then you can use a [GridViewColumn](https://www.telerik.com/products/wpf/documentation/controls/radgridview/columns/columntypes/column-types-basic-column) instead.

* In scenarios with a defined column.CellEditTemplate, the new value of the editor is not available in the arguments of the **CellEditEnded** event that is raised when committing an edit. To get the right value in **e.NewValue**, create your own [Custom Column](https://www.telerik.com/products/wpf/documentation/controls/radgridview/managing-data/how-to/howto-create-custom-editor) and override its **GetNewValueFromEditor** method.

## See Also

* [CellTemplateSelector and CellEditTemplateSelector](https://www.telerik.com/products/wpf/documentation/controls/radgridview/template-selectors/cell-template-selector)
* [Using DataTemplate in Code](https://www.telerik.com/products/wpf/documentation/common-information/using-datatemplates-in-code)
* [Basic Column](https://www.telerik.com/products/wpf/documentation/controls/radgridview/columns/columntypes/column-types-basic-column)
* [Defining Columns](https://www.telerik.com/products/wpf/documentation/controls/radgridview/columns/defining-columns)
* [Customizing Columns](https://www.telerik.com/products/wpf/documentation/controls/radgridview/columns/customizing-columns)
* [Data Formatting](https://www.telerik.com/products/wpf/documentation/controls/radgridview/columns/data-formatting)
