Version

Validating Edited Cell Data in xamDataPresenter

A common task in developing data entry applications is to implement validation logic. Validation logic tests the end user’s input for valid data. The xamDataPresenter™ control raises the EditModeEnding event when the end user is finished editing a cell. This event provides you an opportunity to inject your own custom data validation logic and cancel the cell edit if the validation fails.

Follow these steps to verify that the edited content of a cell can be parsed as an Integer.

  1. Before you start writing any code, you should place using/Imports directives in your code-behind so you don’t need to always type out a member’s fully qualified name.

    In Visual Basic:

    Imports Infragistics.Windows.DataPresenter.Events

    In C#:

    using Infragistics.Windows.DataPresenter.Events;
  1. Wire the EditModeEnding event to xamDataPresenter.

    In XAML:

    <igDP:XamDataPresenter x:Name="XamDataPresenter1"
      ...
      EditModeEnding="XamDataPresenter1_EditModeEnding" />
  1. Create the event handler method in your code-behind.

    In Visual Basic:

    Sub XamDataPresenter1_EditModeEnding(ByVal sender As Object, _
      ByVal e As EditModeEndingEventArgs)
    End Sub

    In C#:

    void XamDataPresenter1_EditModeEnding(object sender, EditModeEndingEventArgs e)
    {
    }

    Notice the method receives the EditModeEndingEventArgs parameter which provides properties containing information specific to edited cell data.

  1. Add your validation logic to the event handler. Verify the edited content of a cell can be parsed as an Integer. If the parse fails, the program rejects the changes using the AcceptChanges property and notifies the end user of the validation failure with a MessageBox.

    In Visual Basic:

    Sub XamDataPresenter1_EditModeEnding(ByVal sender As Object, _
        ByVal e As EditModeEndingEventArgs)
        ' Try to parse the data to see if its valid
        Dim value As Integer
        If Not Int32.TryParse(e.Editor.Text, value) Then
            ' If the parse fails, reject the changes
            e.AcceptChanges = False
            MessageBox.Show("Data Validation Failed. Must enter an Integer")
            e.Cancel = True
        End If
    End Sub

    In C#:

    void XamDataPresenter1_EditModeEnding(object sender, EditModeEndingEventArgs e)
    {
        // Try to parse the data to see if its valid
        int value;
        if (!Int32.TryParse(e.Editor.Text, out value))
        {
            // If the parse fails, reject the changes
            e.AcceptChanges = false;
            MessageBox.Show("Data Validation Failed. Must enter an Integer");
            e.Cancel = true;
        }
    }
  1. Build and run the project. When you enter data that is not of type Integer into any Field, you will be prompted with a MessageBox.

    validating cell data in xamdatapresenter