4
votes

I m facing a problem with WPF DataGrid.

I m using WPF DataGrid & DataGridTemplateColumn (textboxes)

Sometimes I get the following error. Whilst inserting data, I get an error off and on. There is no pattern to reproduce this error. It happens randomly.

System.NullReferenceException : Object Reference not set to an instane of an object.

Detailed Message:

System.NullReferenceException was unhandled by user code
  Message="Object reference not set to an instance of an object."
  Source="PresentationFramework"
  StackTrace:
       at System.Windows.Controls.VirtualizingStackPanel.InsertContainer(Int32 childIndex, UIElement container, Boolean isRecycled)
       at System.Windows.Controls.VirtualizingStackPanel.AddContainerFromGenerator(Int32 childIndex, UIElement child, Boolean newlyRealized)
       at System.Windows.Controls.VirtualizingStackPanel.BringIndexIntoView(Int32 index)
       at Microsoft.Windows.Controls.Primitives.DataGridRowsPresenter.InternalBringIndexIntoView(Int32 index) in C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\Primitives\DataGridRowsPresenter.cs:line 48
       at Microsoft.Windows.Controls.DataGrid.ScrollRowIntoView(Object item) in C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\DataGrid.cs:line 1617
       at Microsoft.Windows.Controls.DataGrid.ScrollCellIntoView(Object item, DataGridColumn column) in C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\DataGrid.cs:line 1638
       at Microsoft.Windows.Controls.DataGrid.ScrollIntoView(Object item, DataGridColumn column) in C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\DataGrid.cs:line 1544
       at Microsoft.Windows.Controls.DataGrid.OnEnterKeyDown(KeyEventArgs e) in C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\DataGrid.cs:line 5473
       at Microsoft.Windows.Controls.DataGrid.OnKeyDown(KeyEventArgs e) in C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\DataGrid.cs:line 5101
       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.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, Boolean isSingleParameter)
       at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
  InnerException:

But when Virtualization is set to False then it works absolutely fine.

Is there something I'm missing? Need a solution.

Thanks in advance.

1

1 Answers

3
votes

Do you insert data by hand or via code?

If by hand then:

1) at what point does the exception pop up, when the edited texbox loses focus?

2) seems you are using the "toolkit" version of datagrid. Is it up to date (Feb 10, is the latest I think)? Have you tried out the builtin datagrid in .NET 4?

If in code then:

If you are trying to for instance call ScrollIntoView for the edited item, then that call must be preceded with datagrid.UpdateLayout(). And if you then try to generate a DataGridRow container for your item, then after getting the DataGridRow you must call UpdateLayout() on it as well, before setting any properties or trying to call methods on it. This is all thanks to the unusual way virtualizing works (at least in the datagrid). If you don't call UpdateLayout as I mentioned, you will get (null ref.) exceptions in some scenarios but not in others.