# Adding New Entries

RadGridView comes with out-of-the-box insert functionality.

There are three ways to insert a new row in RadGridView:

* By pressing the **Insert** key
* By clicking on the row which is shown in the control when the `NewRowPosition` property is set to either **Top** or **Bottom**. To learn more about this approach, check this [article](https://www.telerik.com/products/wpf/documentation/controls/radgridview/features/gridview-new-row).
* By calling the `BeginInsert` method

**Adding new rows with BeginInsert()**

```C#
this.radGridView.BeginInsert();
```

```VB.NET
Me.radGridView.BeginInsert()
```

When a user adds a new item, an empty row is created in which the user can input data.

> caption The newly created row

![Telerik UI for WPF RadGridView showing a newly inserted empty row ready for data entry](https://www.telerik.com/products/wpf/documentation/assets/adbecf50dae12d8ed4c63c3a59e8072e/radgridview_addingnewitems_1.png)

> important If the `IsReadOnly` property of RadGridView is set to **True** or the `CanUserInsertRows` property is set to **False**, no row is added. Additionally, the underlying object should expose a default constructor for an empty row to be added.

## Modifying New Entries

The next step in implementing the adding functionality is to attach event handlers to the `AddingNewDataItem` and the `RowEditEnded` events.

**Add handlers for the AddingNewDataItem and RowEditEnded events**

```XAML
<telerik:RadGridView AddingNewDataItem="radGridView_AddingNewDataItem"
                 RowEditEnded="radGridView_RowEditEnded" />
```

The AddingNewDataItem event is raised before a new row is added to RadGridView. A typical use case would be when you have to set initial values for an initialized object. You can do this by passing an object to the `GridViewAddingNewEventArgs`'s `NewObject` property.

**The AddingNewDataItem event handler**

```C#
private void radGridView_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e)
{
    var employee = new Employee();
    employee.FirstName = "John";
    employee.LastName = "Doe";
    e.NewObject = employee;
}
```

```VB.NET
Private Sub radGridView_AddingNewDataItem(ByVal sender As Object, ByVal e As GridViewAddingNewEventArgs)
    Dim employee = New Employee()
    employee.FirstName = "John"
    employee.LastName = "Doe"
    e.NewObject = employee
End Sub
```

If the ItemsSource is a `DataTable.DefaultView`, you can initialize the newly inserted item as shown below:

**Adding a new item to a DataTable**

```C#
private void radGridView_AddingNewDataItem2(object sender, GridViewAddingNewEventArgs e)
{
    e.Cancel = true;
    var newRow = this.dataSource.DefaultView.AddNew();
    newRow["FirstName"] = "John";
    newRow["LastName"] = "Doe";
    e.NewObject = newRow;
}
```

```VB.NET
Private Sub radGridView_AddingNewDataItem2(sender As Object, e As GridViewAddingNewEventArgs)
    e.Cancel = True
    Dim newRow = Me.dataSource.DefaultView.AddNew()
    newRow("FirstName") = "John"
    newRow("LastName") = "Doe"
    e.NewObject = newRow
End Sub
```

> tip Via the `OwnerGridViewItemsControl` property of the `GridViewAddingNewEventArgs` class you can access the `GridViewItemsControl` for the `RadGridView` that raised the event.

## Committing New Entries

The `RowEditEnded` event is raised when new data is added to RadGridView. This can be done in any of the following ways:

* When the user presses the **Enter** key.
* When the `CommitEdit` method is called.
* When another row is selected.
* When the insert operation is cancelled by pressing the **Escape** key or calling the `CancelEdit` method.

You can access the `EditAction` (**Commit** or **Cancel**) and the `GridViewEditOperationType` (**Insert** or **Edit**) using `GridViewRowEditEndedEventArgs` of the RowEditEnded event. It also allows you to access the new data via the `NewData` property.

**Handling the RowEditEnded event**

```C#
private void radGridView_RowEditEnded(object sender, GridViewRowEditEndedEventArgs e)
{
    if (e.EditAction == GridViewEditAction.Cancel)
    {
        return;
    }
    if (e.EditOperationType == GridViewEditOperationType.Insert)
    {
        //Add the new entry to the data base.
    }
}
```

```VB.NET
Private Sub radGridView_RowEditEnded(ByVal sender As Object, ByVal e As GridViewRowEditEndedEventArgs)
    If e.EditAction = GridViewEditAction.Cancel Then
        Exit Sub
    End If

    If e.EditOperationType = GridViewEditOperationType.Insert Then
        'Add the new entry to the data base.
    End If
End Sub
```

When the new item is committed, it is added to RadGridView's `Items` collection.

> caption The new row

![Telerik UI for WPF RadGridView showing the committed new row after data entry is completed](https://www.telerik.com/products/wpf/documentation/assets/9bb7460a1266261ccf731f4b2a2dc810/radgridview_addingnewitems_2.png)

## See Also

* [Updating an Entry](https://www.telerik.com/products/wpf/documentation/controls/radgridview/managing-data/updating-entry)

* [Deleting an Entry](https://www.telerik.com/products/wpf/documentation/controls/radgridview/managing-data/deleting-entry)

* [Validating](https://www.telerik.com/products/wpf/documentation/controls/radgridview/features/managing-data-validation)

* [Adding Rows](https://www.telerik.com/products/wpf/documentation/controls/radgridview/rows/adding-rows)
