Welcome to the Xceed Community | Help
Community Search  
More Search Options

An attempt was made to call the EndEdit method of an item that is not part of the specified context

Sort Posts: Previous Next
  •  04-28-2011, 6:06 PM Post no. 30266

    An attempt was made to call the EndEdit method of an item that is not part of the specified context

    Hi the Xceed team, 

    With the 4.2 version of the DataGrid, several users of my app sent me bug report with the InvalidOperationException below.

    I cannot reproduce this exception on my dev. machine. The description is "an attempt was made to call the EndEdit method of an item that is not part of the specified context".

    Do you know what this exception means? There is no user code in the stacktrace, and I don't know where to start.

    Thanks for your help. 

     


    Une exception detype 'System.InvalidOperationException' a été levée :
    An attempt was made to call the EndEdit method of an item that is not part ofthe specified context.

    at Xceed.Wpf.DataGrid.DataGridControl.EndEditHelper(DataGridContextdataGridContext)
    at Xceed.Wpf.DataGrid.Views.TableflowViewItemsHost.SetCurrent(Int32 index,Boolean changeColumn, Boolean& isDataRow)
    at Xceed.Wpf.DataGrid.Views.TableflowViewItemsHost.FocusContainerOrPreviousFocusable(UIElementfocusedContainer, Boolean changeColumn)
    at Xceed.Wpf.DataGrid.Views.TableflowViewItemsHost.MoveFocusUp()
    at Xceed.Wpf.DataGrid.Views.TableflowViewItemsHost.HandleUpKey(KeyEventArgs e)
    at Xceed.Wpf.DataGrid.DataGridItemsHost.OnKeyDown(KeyEventArgs e)
    at System.Windows.UIElement.OnKeyDownThunk(Object sender, KeyEventArgs e)
    at System.Windows.Input.KeyEventArgs.InvokeEventHandler(DelegategenericHandler, Object genericTarget)
    at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Objecttarget)
    at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target,RoutedEventArgs routedEventArgs)
    at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgsargs, Boolean reRaised)
    at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender,RoutedEventArgs args)
    at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
    at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
    at System.Windows.Input.InputManager.ProcessStagingArea()
    at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
    at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
    at System.Windows.Interop.HwndKeyboardInputProvider.ReportInput(IntPtr hwnd,InputMode mode, Int32 timestamp, RawKeyboardActions actions, Int32 scanCode,Boolean isExtendedKey, Boolean isSystemKey, Int32 virtualKey)
    at System.Windows.Interop.HwndKeyboardInputProvider.ProcessKeyAction(MSG&msg, Boolean& handled)
    at System.Windows.Interop.HwndSource.CriticalTranslateAccelerator(MSG& msg,ModifierKeys modifiers)
    at System.Windows.Interop.HwndSource.OnPreprocessMessage(Object param)
    at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegatecallback, Object args, Int32 numArgs)
    at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source,Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
    at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority,TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
    at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority,Delegate method, Object arg)
    at System.Windows.Interop.HwndSource.OnPreprocessMessageThunk(MSG& msg,Boolean& handled)
    at System.Windows.Interop.HwndSource.WeakEventPreprocessMessage.OnPreprocessMessage(MSG&msg, Boolean& handled)
    at System.Windows.Interop.ComponentDispatcherThread.RaiseThreadMessage(MSG&msg)
    at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
    at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
    at System.Windows.Threading.Dispatcher.Run()
    at System.Windows.Application.RunDispatcher(Object ignore)
    at System.Windows.Application.RunInternal(Window window)
    at System.Windows.Application.Run(Window window)
  •  05-05-2011, 12:04 PM Post no. 30303 in reply to 30266

    Re: An attempt was made to call the EndEdit method of an item that is not part of the specified context

    I've had the same problem for 2 years or more.  Exactly the same as in this thread http://xceed.com/CS/forums/thread/14706.aspx, InsertionRow in the FixedHeaders and one level of grouping.  Very intermittant and not reproducable. Happens when calling EndEdit() while focus is still in the InsertionRow, i.e. user enters something into the InsertionRow and then does Ctrl-S or clicks the Save button.

    My code is:-

    DataGridContext dataGridContext = DataGridControl.GetDataGridContext(_notesDataGrid);

    Note note = dataGridContext.CurrentItem as Note;

    int index = dataGridContext.CurrentItemIndex;

    try {

       if (_notesDataGrid.IsBeingEdited && _notesDataGrid.HasValidationError == false)

           _notesDataGrid.EndEdit();

    }

    CurrentItem is always null, CurrentItemIndex always -1.

    Xceed, It would be very useful if you could explain what the message means. 

     --Michael

  •  07-27-2011, 4:23 PM Post no. 30800 in reply to 30303

    Re: An attempt was made to call the EndEdit method of an item that is not part of the specified context

    This issue exists again in the version 4.2.11322.13590 of the DataGrid.

    Who knows what does the message "An attempt was made to call the EndEdit method of an item that is not part of the specified context" mean?

    Thanks

  •  03-14-2012, 11:39 AM Post no. 31842 in reply to 30800

    Re: An attempt was made to call the EndEdit method of an item that is not part of the specified context

    +1 for this issue (Xceed 4.3)

     

    While editing a DataGrid, clicking somewhere else fires the xcdg:DataGridControl.LostFocus event. If you monitor this event and call "EndEdit" on sender of the event, you get the above exception if you were in the InsertionRow of a sub-grid (xcdg:DataGridDetailDescription).

     

     A typical Grid_LostFocus event handler looks like this:

     

            private bool HandleFocus_Running = false;
            private void Grid_LostFocus(object sender, RoutedEventArgs e)
            {
                if( HandleFocus_Running ) return; //Avoid reentry
                HandleFocus_Running = true;
     
                try
                {
                    DataGridControl currentGrid = (DataGridControl)sender;
                    DependencyObject visualElement = (DependencyObject)Keyboard.FocusedElement;
     
                    if( visualElement == null )
                    {
                        //The user clicked outside the application so this cell can stay in edit mode for now.
                    }
                    else
                    {
                        DependencyObject nextParent = visualElement;
                        while( nextParent != null && !(visualElement is DataGridControl) )
                        {
                            visualElement = nextParent;
                            nextParent = System.Windows.Media.VisualTreeHelper.GetParent(visualElement);
                        }
     
                        if( visualElement is DataGridControl && currentGrid == visualElement )
                        {
                            //The grid didn't actually lose focus, it just went to another element in the grid. Xceed Datagrids have a bad habit of firing this event inappropriately. Don't do anything.
                        }
                        else if( currentGrid != null )
                        {   //The actions we want to perform when the grid is losing focus
                            currentGrid.EndEdit();  //<--- Throws an error in rare cases
                            currentGrid.SelectedItem = null;
                            currentGrid.CurrentColumn = null;
                        }
                    }
                }
                catchException ex )
                {
                    throw new Exception("XceedDataGrid.Grid_LostFocus() experienced an error.", ex);
                }
                finally
                {
                    HandleFocus_Running = false;
                }
            }

     

    My guess is that the DataGridControl code responsible for invoking the LostFocus event from an InsertionRow is either sending the wrong sender parameter, or not correctly setting the DataGridControl.CurrentContext before doing so.

     

    In any event, I find this can be solved if you simply change the way you call EndEdit();

    Wherever you have the line:

    currentGrid.EndEdit();

    simply replace it with the following:

    currentGrid.CurrentContext.EndEdit(); 

     

    The problem goes away. Enjoy :)


    Alain
  •  05-17-2012, 10:02 AM Post no. 32117 in reply to 31842

    Re: An attempt was made to call the EndEdit method of an item that is not part of the specified context

    Still occurs with DataGrid 4.3.12124.14150 without InsertionRow and without different context.

    A response from the Xceed team would be greatly appreciated.

     

    Thanks. 

View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2011 Xceed Software Inc.