Xceed Grid for WinForms v4.3 Documentation
Using the LeavingEdit event

Welcome to Xceed Grid for WinForms v4.3 > Basic Concepts > Events > Using the LeavingEdit event

The LeavingEdit event is raised after a cell has been edited. It provides information on the cell that has been edited and provides the opportunity to prevent the cell editor from being removed, leaving the cell in edit mode, or to change the new value that is about to be assigned to the cell. 

Keep in mind that the LeavingEdit event will not be raised if the LeaveEdit method is called with its commit parameter set to false.

Basic steps - C#

To subscribe to the LeavingEdit event, the following steps must be performed:

  • Obtain a reference to a Cell object. 

  • Subscribe to the LeavingEdit event of the Cell object using the LeavingEditEventHandler delegate class 

  • Create a new method that will handle the events that are raised. 

  • Place the desired code in the newly created event handler.

Basic steps - VB.NET

To subscribe to the LeavingEdit event, the following steps must be performed:

Demonstration

This example assumes that you are in a Windows application.

VB.NET Copy Code

Imports Xceed.Grid

Dim cell As Cell
For Each cell in gridControl1.DataRowTemplate.Cells
  AddHandler cell.LeavingEdit, AddressOf Me.cell_LeavingEdit
Next cell

' This method will handle the LeavingEdit events that are raised.
Private Sub cell_LeavingEdit( ByVal sender As Object, ByVal e As LeavingEditEventArgs )
  Try
    If( e.NewValue is String ) And ( CType( e.NewValue, String ) = "Hello" ) Then
      e.NewValue = "World"
    End If
  Catch exception As Exception
    MessageBox.Show( exception.ToString() )
  End Try
End Sub

' If you no longer wish to handle the LeavingEdit events that are raised,
' you can unsubscribe from the event notification by doing the following:
RemoveHandler cell.LeavingEdit, AddressOf Me.cell_LeavingEdit

C# Copy Code

using Xceed.Grid;

foreach( Cell cell in gridControl1.DataRowTemplate.Cells )
{
  cell.LeavingEdit += new LeavingEditEventHandler( this.cell_LeavingEdit );
}

// This method will handle the LeavingEdit events that are raised.
private void cell_LeavingEdit( object sender, LeavingEditEventArgs e )
{
  try
  {
    if( ( e.NewValue is string ) && (   ( string )e.NewValue == "Hello" ) )
    {
      e.NewValue = "World";
    }
  }
  catch( Exception exception )
  {
    MessageBox.Show( exception.ToString() );
  }
}

// If you no longer wish to handle the LeavingEdit events that are raised,
// you can unsubscribe from the event notification by doing the following:
cell.LeavingEdit -= new LeavingEditEventHandler( this.cell_LeavingEdit );