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. -
By calling the
BeginInsert
methodAdding 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.
The newly created row
If the
IsReadOnly
property of RadGridView is set to True or theCanUserInsertRows
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
<telerik:RadGridView AddingNewDataItem="radGridView_AddingNewDataItem"
RowEditEnded="radGridView_RowEditEnded" />
GridViewAddingNewEventArgs
's NewObject
property.
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 below:
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 theGridViewAddingNewEventArgs
class you can access theGridViewItemsControl
for theRadGridView
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
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.
The new row