Edit this page

Adding New Entries

RadGridView comes with out-of-the-box insert functionality. In this topic we will discuss:

Inserting New Entries

There are two ways to insert a new row in RadGridView — by pressing the Insert key or by calling the BeginInsert() method.

[C#] Example 1: Adding new rows with BeginInsert()

this.radGridView.BeginInsert();

[VB.NET] Example 1: Adding new rows with BeginInsert()

Me.radGridView.BeginInsert()

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

Figure 1: The newly created row

The newly created row

If the IsReadOnly property of RadGridView is set to True or the CanUserInsertRows property is set to False, no row is added.

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.

[XAML] Example 2: Add handlers for the AddingNewDataItem and RowEditEnded events

<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.

[C#] Example 3: The AddingNewDataItem event handler

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

[VB.NET] Example 3: The AddingNewDataItem event handler

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 in Example 4:

[C#] Example 4: Adding a new item to a DataTable

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] Example 4: Adding a new item to a DataTable

    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

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 EditAction (Commit or Cancel) and GridViewEditOperationType (Insert or Edit) using GridViewRowEditEndedEventArgs of the RowEditEnded event. It also allows you to access the new data via the NewData property.

[C#] Example 5: Handling the RowEditEnded event

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] Example 5: Handling the RowEditEnded event

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.

Figure 2: The new row

The new row

See Also