+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;
}
}
}
catch( Exception 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