# Editing

> By default, the **CanUserEdit** property of **RadVirtualGrid** is set to **True**, thus the editing mechanism is enabled. In order to disable it, set its value to **False**.

**RadVirtualGrid** uses one of two editing paths:

* Without a **DataProvider**, handle **CellValueNeeded**, **EditorNeeded**, **EditorValueChanged**, and **CellEditEnded** yourself. The control does not persist edited values in your data source automatically.

* With a **DataProvider**, the provider creates the default editor, supplies cell values, determines whether a column is read-only, and handles the edit lifecycle. The default **DataProvider** automatically writes committed values to its source and updates the grid.

The editor lifecycle is:

1. The user enters edit mode through an edit trigger or a call to **BeginEdit**.
2. **EditorNeeded** is raised. Create an editor, assign it to **Editor**, and assign the editor dependency property to **EditorProperty**.
3. The editor changes its dependency property. **EditorValueChanged** receives the current value.
4. The edit ends through **CommitEdit**, **CancelEdit**, or the configured **ActionOnLostFocus** behavior. **CellEditEnded** receives the final value and an **EditAction**.
5. For a committed edit, persist the value and call **PushCellValue** when you manage the data source directly. A **DataProvider** does this automatically when **ShouldPushEditValueToGrid** is **True**.

* [Editing paths](https://www.telerik.com/products/wpf/documentation/controls/radvirtualgrid/features/editing#editing-paths)

* [Events](https://www.telerik.com/products/wpf/documentation/controls/radvirtualgrid/features/editing#events)

* [Methods](https://www.telerik.com/products/wpf/documentation/controls/radvirtualgrid/features/editing#methods)

* [Edit Triggers](https://www.telerik.com/products/wpf/documentation/controls/radvirtualgrid/features/editing#edittriggers)

* [Action on LostFocus](https://www.telerik.com/products/wpf/documentation/controls/radvirtualgrid/features/editing#action-on-lostfocus)

## Editing Paths

### Direct Event Handling

Use direct event handling when you populate the grid through **InitialRowCount** and **InitialColumnCount** and keep the data outside a **DataProvider**. Handle **CellValueNeeded** to display values, create the editor in **EditorNeeded**, and persist the value in **CellEditEnded**.

The following example uses a **TextBox** for every editable cell. Replace the `SaveValue` implementation with the code that updates your data source.

```C#
using System.Windows.Controls;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.VirtualGrid;

private void VirtualGrid_CellValueNeeded(object sender, CellValueEventArgs e)
{
    e.Value = this.GetValue(e.RowIndex, e.ColumnIndex);
}

private void VirtualGrid_EditorNeeded(object sender, EditorNeededEventArgs e)
{
    var editor = new TextBox
    {
        Text = e.TextInput ?? this.GetValue(e.RowIndex, e.ColumnIndex)?.ToString()
    };

    e.Editor = editor;
    e.EditorProperty = TextBox.TextProperty;
}

private void VirtualGrid_CellEditEnded(object sender, CellEditEndedEventArgs e)
{
    if (e.EditAction != VirtualGridEditAction.Commit)
    {
        return;
    }

    this.SaveValue(e.RowIndex, e.ColumnIndex, e.Value);
    this.VirtualGrid.PushCellValue(e.RowIndex, e.ColumnIndex, e.Value);
}
```

The **PushCellValue** call updates the value that **RadVirtualGrid** renders. It does not replace persistence in the underlying data source, so call both operations after a committed edit.

### DataProvider editing

Use a **DataProvider** when the grid should read and write values through an item collection. The default provider creates a **TextBox** editor, uses item properties to determine the editor value, and treats read-only item properties as non-editable.

The provider's **ShouldPushEditValueToGrid** property returns **True** by default. On a committed edit, **RadVirtualGrid** calls **PushCellValueToSource** and **PushCellValue**. Override this property when your provider owns synchronization and must handle the source and grid update itself.

The provider also receives **OnCellValueNeeded**, **OnEditorNeeded**, **OnEditorValueChanged**, and **OnCellEditEnded** calls. When a **DataProvider** is assigned, the corresponding public editing events are not raised by **RadVirtualGrid**; override the provider methods instead.

```C#
using System.Collections;
using System.Windows;
using System.Windows.Controls;
using Telerik.Windows.Controls.VirtualGrid;

public class ProductDataProvider : DataProvider
{
    public ProductDataProvider(IEnumerable source)
        : base(source)
    {
    }

    protected override void CreateEditor(out FrameworkElement editor, out DependencyProperty editorProperty, object propertyValue, int columnIndex)
    {
        editor = new TextBox
        {
            Text = propertyValue?.ToString()
        };
        editorProperty = TextBox.TextProperty;
    }

    protected internal override void OnCellEditEnded(CellEditEndedEventArgs e)
    {
        if (e.EditAction == VirtualGridEditAction.Commit)
        {
            // Persist here only when the provider owns custom conversion or storage.
        }
    }
}
```

If the custom provider does not override **ShouldPushEditValueToGrid**, the base provider still converts the value to the item property type, writes it through the property descriptor, and refreshes the grid after a commit. Do not persist a cancelled value: **CellEditEndedEventArgs.EditAction** is **Cancel** when the edit is cancelled.

## Events

> important The public editing events are not raised when the **DataProvider** mechanism is used for populating the data of **RadVirtualGrid**. Override the corresponding **DataProvider** methods instead. More information can be found in the [Custom DataProvider](https://www.telerik.com/products/wpf/documentation/controls/radvirtualgrid/features/custom-dataprovider) topic.

### EditorNeeded

Through this event a custom editor for handling the editing operation can be defined. The event arguments expose the following properties:

* **ColumnIndex**: Provides information regarding the column index of the editor

* **RowIndex**: Provides information regarding the row index of the editor

* **Editor**: The control that will be used for the editing operation

* **EditorProperty**: The editor's dependency property that is to be edited

* **TextInput**: Gets the string containing the entered text when the **TextInput** edit trigger starts an edit. Use this value when you want the first typed character to replace the original cell value.

> The property of the editor that is being edited needs to be set manually as well.

**Example 1: Handling the EditorNeeded event**

```C#
private void VirtualGrid_EditorNeeded(object sender,
        Telerik.Windows.Controls.VirtualGrid.EditorNeededEventArgs e)
{
    TextBox tb = new TextBox();

    e.Editor = tb;
    tb.Text = String.Format("{0}.{1}", e.RowIndex, e.ColumnIndex);
    e.EditorProperty = TextBox.TextProperty;
}
```

> Most input controls (MaskedTextInput, RadNumericUpDown, RadComboBox, etc.) in the Material, Fluent and Transparent themes have an opacity applied. This means that you can see through them which could lead to a visual glitch when used as editors in RadVirtualGrid. To avoid this set the Background property of the editor to a solid color when you create it in the event handler.

### EditorValueChanged

This event is triggered each time the editor dependency property changes. With a **DataProvider**, override **DataProvider.OnEditorValueChanged** instead of handling this public event.

* **ColumnIndex**: Provides information regarding the column index of the editor

* **RowIndex**: Provides information regarding the row index of the editor

* **Value**: Provides the current editor value.

**Example 2: Handling the EditorValueChanged event**

```C#
private void VirtualGrid_EditorValueChanged(object sender,
        Telerik.Windows.Controls.VirtualGrid.CellValueEventArgs e)
{

}
```

### CellEditEnded

The event is raised when the edit ends. The event can result from **CommitEdit**, **CancelEdit**, or **ActionOnLostFocus**. With a **DataProvider**, override **DataProvider.OnCellEditEnded** instead of handling this public event.

* **ColumnIndex**: Provides information regarding the column index of the editor

* **RowIndex**: Provides information regarding the row index of the editor

* **Value**: Provides the current or final editor value.

* **EditAction**: Indicates whether the edit was committed through **VirtualGridEditAction.Commit** or cancelled through **VirtualGridEditAction.Cancel**.

**Example 3: Handling the CellEditEnded event**

```C#
private void VirtualGrid_CellEditEnded_1(object sender, CellEditEndedEventArgs e)
{

}
```

## Methods

### BeginEdit

Puts **RadVirtualGrid** into edit mode for given cell coordinates. The method will work only if the control is not already in edit mode.

* **BeginEdit(int rowIndex, int columnIndex)**

### CancelEdit

Calling this method causes **RadVirtualGrid** to cancel the current edit, revert to the original value and exit edit mode.

* **CancelEdit()**

### CommitEdit

Commits the current edit and exits edit mode.

* **CommitEdit()**

### PushCellValue

When an edit is committed through direct event handling, persist the new value in the underlying source and call **PushCellValue** to update **RadVirtualGrid**. The method only updates the value rendered by the control; it does not write to your source. A **DataProvider** calls the source and grid update methods automatically when **ShouldPushEditValueToGrid** is **True**.

* **PushCellValue(int rowIndex, int columnIndex, object value)**

**Example 4: Updating RadVirtualGrid with the modified data**

```C#
private void VirtualGrid_CellEditEnded(object sender,
        Telerik.Windows.Controls.VirtualGrid.CellValueEventArgs e)
{
    this.VirtualGrid.PushCellValue(e.RowIndex, e.ColumnIndex, e.Value);
}
```

## EditTriggers

**RadVirtualGrid** provides a mechanism through which the way the control enters edit mode can be controlled. This is done through the **EditTriggers** property. It is a flag enumeration that has the following values:

* **CellClick**: A **single click** will put the cell into edit mode.

* **CurrentCellClick**: A click on the current cell will put it into edit mode.

* **Default**: The default setting which combines the **CurrentCellClick**, **F2** and **TextInput** values.

* **F2**: Pressing **F2** on a cell will put it into edit mode.

* **None**: No action will put the cell in edit mode.

* **TextInput**: Any text input will put the cell into edit mode. When the control is populated with data manually, instead of with **DataProvider**, the **EditorNeeded** event will have the **TextInput** property of the event arguments equal to the key with which enter mode is entered.

## Action on LostFocus

The action that **RadVirtualGrid** takes when its element loses focus can be manipulated through the **ActionOnLostFocus** property. It has the following three values.

* **CancelEdit**: Cancels the current edit when the focus of the edited field is lost.

* **CommitEdit**: Commits the current edit when the focus of the edited field is lost.

* **None**: No specific action will be taken when the focus of the edited field is lost.

## See Also

* [Insert and Remove Data](https://www.telerik.com/products/wpf/documentation/controls/radvirtualgrid/features/insert-and-remove-data)
* [Pinned Rows and Columns](https://www.telerik.com/products/wpf/documentation/controls/radvirtualgrid/features/pinned-rows-and-columns)
