【发布时间】:2019-02-02 19:22:00
【问题描述】:
我正在捕获用户在 WPF 中完成我的 DataGrid 中的单元格的编辑并尝试更新 DataTable 以反映这一点。
但是,代码进入“中断模式”,然后就关闭了。
private void DataGridView1_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
DataGridColumn col1 = e.Column;
DataGridRow row1 = e.Row;
int row_index = ((System.Windows.Controls.DataGrid)sender).ItemContainerGenerator.IndexFromContainer(row1);
int col_index = col1.DisplayIndex;
var itemtext = ((System.Windows.Controls.TextBox)e.EditingElement).Text;
if(dt.Rows[row_index].ItemArray[col_index].ToString()!= itemtext)
{
DataGridView1.DataContext = null;
dt.Rows[row_index][col_index] = itemtext;
DataGridView1.DataContext = dt;
}
}
有人知道是什么原因造成的吗?
dt 是 DataTable。
每次编辑单元格时我都更新DataContext 的原因是我有代码说明如果单元格值超过 33 个字符,则将单元格着色为红色,所以每当编辑单元格时,我想要更新颜色以反映这一点。
更新
我查看了输出调试,发现如下错误
PresentationCore.dll 中出现“System.ArgumentNullException”类型的未处理异常 值不能为空。
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.UpdateCellAutomationValueHolder(DataGridCell cell)
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.CommitAnyEdit()
at System.Windows.Controls.DataGrid.OnEnterKeyDown(KeyEventArgs e)
at System.Windows.Controls.DataGrid.OnKeyDown(KeyEventArgs e)
at System.Windows.UIElement.OnKeyDownThunk(Object sender, KeyEventArgs e)
at System.Windows.Input.KeyEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
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.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(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(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.ThreadMessageEventHandler.Invoke(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.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run(Window window)
at System.Windows.Application.Run()
at DMHotFolderMain.App.Main()
我发现只有当DataContext 为空并且同时调用更新DataTable 时才会崩溃。
我尝试将这些行注释掉,它们可以单独工作,但不能一起工作。
【问题讨论】:
-
您是否收到任何错误消息?
-
是的,我刚刚在输出中看到了,我会更新问题
-
你找到解决方案了吗?