New to Telerik UI for WPF? Download free 30-day trial

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

    Example 1: Showing the GridViewNewRow

        <telerik:RadGridView NewRowPosition="Bottom" /> 
    

    Figure 1: The GridViewNewRow WPF RadGridView The GridViewNewRow

  • By calling the BeginInsert() method

    Example 2: Adding new rows with BeginInsert()

        this.radGridView.BeginInsert(); 
    
        Me.radGridView.BeginInsert() 
    

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

Figure 2: The newly created row

Telerik WPF DataGrid Adding New Items 1

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.

Example 3: 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.

Example 4: 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; 
} 
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:

Example 5: 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; 
} 
    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.

Example 6: 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. 
    } 
} 
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 3: The new row

Telerik WPF DataGrid Adding New Items 2

See Also

In this article