Welcome to the Xceed Community Sign in | Join | Help
Community Search  

Inline Editing Exception - An attempt was made to edit an item that is not part of the specified context

  •  08-12-2008, 8:28 AM

    Inline Editing Exception - An attempt was made to edit an item that is not part of the specified context

    Hi,

    I am using Xceed Datagrids for WPF 2.0 in my application and I have also provided support for in-line editing of data grids. Please find below the details for the problem that I am facing currently.

     Consider there are 3 columns in a grid - ID, FromRegion and ToRegion. All 3 are editable and editor is a textbox.  

    1. Click on ‘FromRegion’ cell.
    2. Modify the value so that there is no validation error, say 20.
    3. Now click on any cell of the same row, in this case “ToRegion”. Points to take care of in this step:
      1. After editing the cell in step 2, do not press ‘Enter’ key or do not click on any other row in the grid.
      2. This is because as soon as we do the above, the modified value of step 2 will get committed to the underlying data source. We do not want this.
    4. After clicking in the different cell of the same row, i.e. “ToRegion”, modify its value in such a manner that it will cause a validation error. For e.g. enter -10.  

    The following code is called on pressing of EndEdit button.

         protected void btnEndEdit_Click(object sender, RoutedEventArgs args) {

             try{

                wfhGrid.EndEdit();

             }

             catch (DataGridException e1)      {

                if (wfhGrid.HasValidationError)   {

                   MessageBox.Show("Exception after Validation called. CancelEdit() to be called and application is about to crash. Exception Reason - '" + e1.Message + "'");

                   wfhGrid.CancelEdit();

                }

                return ;

             }

             catch (Exception e)

             {

                MessageBox.Show("Exception. Validation already called. Exception Reason - '" + e.Message + "'");

                return;

             }

            

          }

     Case 1:

    1. After modifying “ToRegion” to -10, don’t click anywhere outside the cell. Directly click on “Call EndEdit” button.
    2. On call of “wfhGrid.EndEdit();” function, valid exception is thrown (since there is a validation error). But then on call of “wfhGrid.CancelEdit();” the following exception is being thrown.

    Exception Type: “InvalidOperationException”

    Exception Message: “An attempt was made to edit an item that is not part of the specified context.”

     Case 2:

    1. After modifying “ToRegion” to -10, click anywhere outside the cell so that the cell gets painted with a red border.
    2. Now click on “Call EndEdit” button.
    3. This time the exception is thrown on call of “wfhGrid.EndEdit();” function itself.

    Exception Type: “InvalidOperationException”

    Exception Message: “An attempt was made to edit an item that is not part of the specified context.”

     Can you please let me know why this is happening and how do I resolve this problem.

     

    I am attaching the source code for reference:

     

    using System;

    using System.Windows;

    using System.Windows.Controls;

    using System.Windows.Data;

    using Xceed.Wpf.DataGrid;

    using Xceed.Wpf.DataGrid.Markup;

    using Xceed.Wpf.DataGrid.Views;

    using Xceed.Wpf.DataGrid.ValidationRules;

    using System.Globalization;

    namespace DataGridTestApp

    {

    /// <summary>

    /// Interaction logic for Window1.xaml

    /// </summary>

    public partial class Window1 : System.Windows.Window

    {

    AircraftCrew.AircraftCrewDataTable _dtAircraftCrew;

    public Window1()

    {

    _dtAircraftCrew = new AircraftCrew.AircraftCrewDataTable(); // This is a typed dataset

    InitializeComponent();

    InitializeGrid();

    }

    private void InitializeGrid()

    {

    AircraftCrew.AircraftCrewRow row;

    row = _dtAircraftCrew.NewAircraftCrewRow();

    row.ID = Guid.NewGuid();

    row.FromRegion = 10;

    row.ToRegion = 10;

    _dtAircraftCrew.AddAircraftCrewRow(row);

    _dtAircraftCrew.AcceptChanges();

    wfhGrid.ItemsSource = new BindingListCollectionView(_dtAircraftCrew.DefaultView);

    wfhGrid.ReadOnly = false;

    wfhGrid.EditTriggers = EditTriggers.CellIsCurrent;

    wfhGrid.CellEditorDisplayConditions = CellEditorDisplayConditions.MouseOverCell;

    wfhGrid.ValidationMode = ValidationMode.CellEndingEdit;

    Column column = wfhGrid.Columns["FromRegion"];

    column.CellValidationRules.Add(new MinimumValueCellValidationRule(0));

    column = wfhGrid.Columns["ToRegion"];

    column.CellValidationRules.Add(new MinimumValueCellValidationRule(0));

    }

     

    public class MinimumValueCellValidationRule : CellValidationRule

    {

    private double m_min;

    public MinimumValueCellValidationRule(double min)

    {

    m_min = min;

    }

    // The minimum value of the cell content

    public override ValidationResult Validate(object value, CultureInfo culture, CellValidationContext context)

    {

    double number = Convert.ToDouble(value, CultureInfo.CurrentCulture);

    if (number < m_min)

    {

    string message = string.Format("Error");

    return new ValidationResult(false, message);

    }

    return ValidationResult.ValidResult;

    }

    }

     

    protected void btnEndEdit_Click(object sender, RoutedEventArgs args)

    {

    try

    {

    wfhGrid.EndEdit();

    }

    catch (DataGridException e1)

    {

    if (wfhGrid.HasValidationError)

    {

    MessageBox.Show("Exception after Validation called. CancelEdit() to be called and application is about to crash. Exception Reason - '" + e1.Message + "'");

    wfhGrid.CancelEdit();

    }

    return ;

    }

    catch (Exception e)

    {

    MessageBox.Show("Exception. Validation already called. Exception Reason - '" + e.Message + "'");

    return;

    }

     

    }

    }

    }

View Complete Thread
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2008 Xceed Software Inc.