Click here to Skip to main content
15,886,830 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm using WPF Datagrid and I've binding the itemssource as datatable.

I'm frequently editing the cell details and copy the cell value from one to another. During this process, I'm getting the below error while edit the cell event in WPF DataGrid.


The below Code I Have used for editing Cell Value
Cell Edit Code :

C#
<pre>private void AutoBook_Datagrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            try
            {
                if (e.EditAction == DataGridEditAction.Commit)
                {
                    int primaryKey = 0;
                    DataRowView editedRow = (DataRowView)mainWindow.autoBook_Datagrid.SelectedItems[0];
                    if (editedRow != null && !string.IsNullOrEmpty(editedRow.Row["ID"].ToString()))
                    {
                        primaryKey = Convert.ToInt32(editedRow.Row["ID"]);
                    }
                    if (e.EditingElement.ToString().Contains("TextBox"))
                    {
                        if (e.EditingElement != null)
                        {
                            UpdateTextBoxValue(e.EditingElement as TextBox, e.Column.Header.ToString(), primaryKey);
                        }
                    }
                    else if (e.EditingElement.ToString().Contains("CheckBox"))
                    {
                        if (e.EditingElement != null)
                        {
                            UpdateCheckBoxValue(e.EditingElement as CheckBox, e.Column.Header.ToString(), primaryKey);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


XAML Binding :

C#
<pre><DataGrid
                        x:Name="autoBook_Datagrid"
                        HorizontalScrollBarVisibility="Auto"
                        VerticalScrollBarVisibility="Auto"
                        ScrollViewer.CanContentScroll="True"
                        BorderBrush="{StaticResource GreenBrush}"
                        HorizontalAlignment="Left"
                        VerticalAlignment="Top"
                        RowHeight="25"
                        GridLinesVisibility="All"
                        ClipboardCopyMode="IncludeHeader"
                        ItemsSource="{Binding DynamicBindingTable, UpdateSourceTrigger=PropertyChanged}"
                        CanUserAddRows="{Binding CanAddNewRows, UpdateSourceTrigger=PropertyChanged}"
                        VirtualizingPanel.VirtualizationMode="Recycling"
                        VirtualizingPanel.ScrollUnit="Pixel"
                        EnableColumnVirtualization="True"
                        EnableRowVirtualization="True"
                        FocusVisualStyle="{StaticResource ButtonFocusVisual}"
                            CanUserDeleteRows="False"
                        >


Error Message :

Unhandled Exception: System.ArgumentNullException: Value cannot be null.
Parameter name: element
at System.Windows.Automation.Peers.UIElementAutomationPeer.FromElement(UIElement element)
at System.Windows.Controls.DataGrid.CellAutomationValueHolder.TrackValue()
at System.Windows.Controls.DataGrid.ReleaseCellAutomationValueHolders()
at System.Windows.Controls.DataGrid.OnExecutedCommitEdit(ExecutedRoutedEventArgs e)
at System.Windows.Controls.DataGrid.OnExecutedCommitEdit(Object sender, ExecutedRoutedEventArgs e)


Could you please help me to resolve this issue?

Regards,
Arunkumar Murugesan

What I have tried:

I couldn't find the solution. Could you please help me to get out of this issue?
Posted
Updated 20-Mar-20 22:37pm
Comments
Richard Deeming 23-Mar-20 16:12pm    
catch (Exception ex)
{
    throw ex;
}

Don't do that. You've just thrown away the stack trace of the exception, making it much harder to track down the true source of any errors.

If you really must re-throw an exception, just use throw; instead of throw ex;:
catch (Exception)
{
    throw;
}

But in this case, since you're not doing anything with the exception, just don't catch it. Remove the try..catch block, and let the exceptions propagate normally.
Richard Deeming 23-Mar-20 16:15pm    
The error looks like a WPF bug. Someone on StackOverflow was getting the same error back in 2018, but there's no real solution there:
c# - Entering break mode on DataGrid CellEditEnding event - Stack Overflow[^]

1 solution

The error is clear: System.ArgumentNullException: Value cannot be null., you are passing a null value in one of the arguments to a method call. There is no simple solution to such a problem as you need to find out which value is null. The only way is to use your debugger to step through the code until it triggers the exception. You can then examine all the variables to find the offending one.
 
Share this answer
 
Comments
Arunkumar Murugesan 21-Mar-20 9:06am    
Hi Richard, Thanks for your reply.

Actually there is no null value I have passed in the arguments. After executed the events only this exception thrown. That's why I couldn't track the exact problem.

I have null check for the elements. But still it getting exception. Exception not thrown in catch block also.

Error Details


System.ArgumentNullException
HResult=0x80004003
Message=Value cannot be null.
Parameter name: element
Source=PresentationCore
StackTrace:
at System.Windows.Automation.Peers.UIElementAutomationPeer.FromElement(UIElement element)
at System.Windows.Controls.DataGrid.CellAutomationValueHolder.TrackValue()
at System.Windows.Controls.DataGrid.ReleaseCellAutomationValueHolders()
at System.Windows.Controls.DataGrid.OnExecutedCommitEdit(ExecutedRoutedEventArgs e)
at System.Windows.Controls.DataGrid.OnExecutedCommitEdit(Object sender, ExecutedRoutedEventArgs e)
at System.Windows.Input.CommandBinding.OnExecuted(Object sender, ExecutedRoutedEventArgs e)
at System.Windows.Input.CommandManager.ExecuteCommandBinding(Object sender, ExecutedRoutedEventArgs e, CommandBinding commandBinding)
at System.Windows.Input.CommandManager.FindCommandBinding(Object sender, RoutedEventArgs e, ICommand command, Boolean execute)
at System.Windows.Input.CommandManager.OnExecuted(Object sender, ExecutedRoutedEventArgs e)
at System.Windows.UIElement.OnExecutedThunk(Object sender, ExecutedRoutedEventArgs e)
at System.Windows.Input.ExecutedRoutedEventArgs.InvokeEventHandler(Delegate genericHandler, Object target)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
at System.Windows.Input.RoutedCommand.ExecuteImpl(Object parameter, IInputElement target, Boolean userInitiated)
at System.Windows.Input.RoutedCommand.Execute(Object parameter, IInputElement target)
at System.Windows.Controls.DataGrid.EndEdit(RoutedCommand command, DataGridCell cellContainer, DataGridEditingUnit editingUnit, Boolean exitEditMode)
at System.Windows.Controls.DataGrid.OnCurrentCellChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
at System.Windows.DependencyObject.SetCurrentValueInternal(DependencyProperty dp, Object value)
at System.Windows.Controls.DataGrid.set_CurrentCellContainer(DataGridCell value)
at System.Windows.Controls.DataGrid.set_FocusedCell(DataGridCell value)
at System.Windows.Controls.DataGridCell.OnAnyGotFocus(Object sender, RoutedEventArgs e)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Win
Richard MacCutchan 21-Mar-20 9:11am    
There is still a null value, as shown in the error message. I am afraid there is no simple way to find this type of error, other than lots of debugging.
Member 14918235 26-Jan-21 11:40am    
I am having the same issue in my EndEdit event handler as well. No null values, throws the except at automation peers. Been stuck on this for over a week. Did you ever find a solution?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900