Edit Events
The edit events are ment to support the editing data process. They occur when the data in the RadGridView is about to be edited or has been already edited by the user.
Currently the edit events occur on row and cell level. To get notified use the following events exposed by the RadGridView control:
Edit Events Lifecycle
It is important to know that each one of the edit events is fired only when the user is trying to edit data in RadGridView control.
The BeginningEdit event always occurs when the user is about to edit the data. The BeginningEdit event allows you to stop the edit process by setting the boolean property Cancel to True. Note that regarding the fact that the edit process is cancelled, the BeginningEdit event will still be raised. If not canceled, the PreparingCellForEdit event is fired. You can access the default editing element there and set its properties if needed. The PreparingCellForEdit can be canceled too, thus preventing the PreparedCellForEdit event to fire.
The CellEditEnded event is always fired before RowEditEnded event.
BeginningEdit Event
The BeginningEdit event occurs when the cell is about to enter into EditMode. The BeginningEdit event handler receives two arguments:
The sender argument contains the RadGridView. This argument is of type object, but can be cast to the RadGridView type.
A GridViewBeginningEditRoutedEventArgs object. This object has the following properties:
Cancel - gets or sets the value indicating whether the event should be canceled.
Cell - gets or sets the cell.
Row - gets or sets the row.
You can subscribe to the BeginningEdit event declaratively or runtime like this:
<telerik:RadGridView x:Name="radGridView" BeginningEdit="radGridView_BeginningEdit"/>
this.radGridView.BeginningEdit += radGridView_BeginningEdit;
AddHandler Me.radGridView.BeginningEdit, AddressOf radGridView_BeginningEdit
The BeginningEdit event is cancelable:
private void radGridView_CancelBeginningEdit(object sender, Telerik.Windows.Controls.GridViewBeginningEditRoutedEventArgs e)
{
e.Cancel = true;
}
Private Sub radGridView_CancelBeginningEdit(ByVal sender As Object, ByVal e As Telerik.Windows.Controls.GridViewBeginningEditRoutedEventArgs)
e.Cancel = True
End Sub
The example below uses the BeginningEdit event to show a tool tip when the user tries to edit a cell from a certain column:
private void radGridView_BeginningEdit(object sender, Telerik.Windows.Controls.GridViewBeginningEditRoutedEventArgs e)
{
if (e.Cell.Column.UniqueName == "CustomerID")
{
ToolTipService.SetToolTip(e.Cell, "Editing the ID may result in inconsistency in the database");
}
}
Private Sub radGridView_BeginningEdit(ByVal sender As Object, ByVal e As Telerik.Windows.Controls.GridViewBeginningEditRoutedEventArgs)
If e.Cell.Column.UniqueName = "CustomerID" Then
ToolTipService.SetToolTip(e.Cell, "Editing the ID may result in inconsistency in the database")
End If
End Sub
PreparingCellForEdit Event
The PreparingCellForEdit event fires after the BeginningEdit event. It allows you to access the default editor and initialize some of its properties if needed. The event handler receives two arguments:
The sender argument contains RadGridView. This argument is of type object, but can be cast to the RadGridView type.
A GridViewPreparingCellForEditEventArgs object. This object has the following properties:
Cancel - gets or sets the value indicating whether the event should be canceled.
Column - gets the column that the cell belong to.
Row - gets the row that the cell belong to.
EditingElement - gets the default editing element
The example below uses the PreparingCellForEdit event to access the underlying TextBox editing element and set its TextWrapping property to Wrap:
private void clubsGrid_PreparingCellForEdit(object sender, GridViewPreparingCellForEditEventArgs e)
{
if ((string)e.Column.Header == "Name")
{
var tb = e.EditingElement as TextBox;
tb.TextWrapping = TextWrapping.Wrap;
}
}
Private Sub clubsGrid_PreparingCellForEdit(sender As Object, e As GridViewPreparingCellForEditEventArgs)
If DirectCast(e.Column.Header, String) = "Name" Then
Dim tb = TryCast(e.EditingElement, TextBox)
tb.TextWrapping = TextWrapping.Wrap
End If
End Sub
PreparedCellForEdit Event
The PreparedCellForEdit event fires after the PreparingCellForEdit event (if not canceled). When fired, the editing element is already prepared, e.g. the text is already selected. This is the place where you can alter this behavior.
The event handler receives two arguments:
The sender argument contains the RadGridView. This argument is of type object, but can be cast to the RadGridView type.
A GridViewPreparingCellForEditEventArgs object. This object has the following properties:
Column - gets the column that the cell belong to.
Row - gets the row that the cell belong to.
EditingElement - gets the default editing element
The example below uses the PreparedCellForEdit event to prevent the selection of the text inside the editing element:
private void clubsGrid_PreparedCellForEdit(object sender, GridViewPreparingCellForEditEventArgs e)
{
if ((string)e.Column.Header == "Name")
{
var tb = e.EditingElement as TextBox;
//remove the selection of the text
tb.SelectionLength = 0;
}
}
Private Sub clubsGrid_PreparedCellForEdit(sender As Object, e As GridViewPreparingCellForEditEventArgs)
If DirectCast(e.Column.Header, String) = "Name" Then
Dim tb = TryCast(e.EditingElement, TextBox)
'remove the selection of the text
tb.SelectionLength = 0
End If
End Sub
CellEditEnded Event
The CellEditEnded occurs when cell validation is passed successfully and new data is committed to the RadGridView.ItemsSource. The CellEditEnded event handler receives two arguments:
The sender argument contains the RadGridView. This argument is of type object, but can be cast to the RadGridView type.
A GridViewCellEditEndedEventArgs object. This object has the following properties:
Cell - Gets the edited cell.
EditAction - Gets the edit action.
EditingElement - Gets the editor element.
NewData - Gets the new data.
OldData - Gets the old data.
If the edited cell's column has a CellEditTemplate set, the e.NewData property will always be null. You can get the new value through the e.EditingElement parameter instead.
The EditAction property is a GridViewEditAction enumeration, which exposes the following values:
Cancel
Commit
You can subscribe to the CellEditEnded event declaratively or runtime like this:
<telerik:RadGridView x:Name="radGridView" CellEditEnded="radGridView_CellEditEnded"/>
this.radGridView.CellEditEnded += radGridView_CellEditEnded;
AddHandler Me.radGridView.CellEditEnded, AddressOf radGridView_CellEditEnded
The example below uses the CellEditEnded event to show a message box containing details of the cell being edited - like column's Unique name and the new value of the cell:
private void radGridView_CellEditEnded(object sender, Telerik.Windows.Controls.GridViewCellEditEndedEventArgs e)
{
Employee editedEmployee = e.Cell.DataContext as Employee;
string propertyName = e.Cell.Column.UniqueName;
MessageBox.Show(string.Format("Property {0} is edited and newValue is {1}", propertyName, e.NewData));
}
Private Sub radGridView_CellEditEnded(ByVal sender As Object, ByVal e As Telerik.Windows.Controls.GridViewCellEditEndedEventArgs)
Dim editedEmployee As Employee = TryCast(e.Cell.DataContext, Employee)
Dim propertyName As String = e.Cell.Column.UniqueName
MessageBox.Show(String.Format("Property {0} is edited and newValue is {1}", propertyName, e.NewData))
End Sub
RowEditEnded Event
The RowEditEnded event occurs when row validation passed successfully and new data is committed to the RadGridView.ItemsSource.The RowEditEnded event handler receives two arguments:
The sender argument contains the RadGridView. This argument is of type object, but can be cast to the RadGridView type.
A GridViewRowEditEndedEventArgs object. This object has the following properties:
NewData - Gets the new data of the edited row.
Row - Gets the edited GridViewRow.
EditAction - Gets the edit action.
EditOperationType - Gets the edit operation type.
The EditOperationType property is a EditOperationType enumeration, which exposes the following values:
Insert
Edit
You can subscribe to the RowEditEnded event declaratively or runtime like this:
<telerik:RadGridView x:Name="radGridView" RowEditEnded="radGridView_RowEditEnded"/>
this.radGridView.RowEditEnded += radGridView_RowEditEnded;
AddHandler Me.radGridView.RowEditEnded, AddressOf radGridView_RowEditEnded
The example below uses the RowEditEnded to display the new data of the edited row in a text block. It also assumes that the grid is bound to List of Employee objects:
private void radGridView_RowEditEnded(object sender, Telerik.Windows.Controls.GridViewRowEditEndedEventArgs e)
{
Employee newEmployee = e.NewData as Employee;
if (newEmployee != null)
{
textBlock1.Text = "e.NewData contains: " + newEmployee.ToString();
}
}
Private Sub radGridView_RowEditEnded(ByVal sender As Object, ByVal e As Telerik.Windows.Controls.GridViewRowEditEndedEventArgs)
Dim newEmployee As Employee = TryCast(e.NewData, Employee)
If newEmployee IsNot Nothing Then
textBlock1.Text = "e.NewData contains: " & newEmployee.ToString()
End If
End Sub
The CellEditEnded event is always fired before RowEditEnded event.