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 :
<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 :
<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?