Setting DisplayIndex Exception

Feb 23, 2009 at 5:59 PM
When setting the display index of my columns I am getting an ArgumentOutofRangeException.  I am restoring the column order and visibility based on some information I received from an external source.  The code to set the display index and visible properties is called from the Loaded event on a user control.  The code is below.  This problem does not occur all the time only when I have certain columns in a certain order.  I have found a HACK to work around this but was hopeful there is a solution to this or I am doing something wrong.  Thanks for any help

              List<PersonColumnInfo> columns = info[listId];

              var orderColumns = columns.OrderBy(p => p.CurrentColumnIndex);

              foreach (var item in orderColumns)
              {
                grid.Columns[item.OrigColumnIndex].DisplayIndex = item.CurrentColumnIndex;

                if (!item.IsVisible)
                {
                  grid.Columns[item.OrigColumnIndex].Visibility = Visibility.Hidden;
                }

                grid.Columns[item.OrigColumnIndex].Width = new DataGridLength(Convert.ToDouble(item.Width));
              }

System.ArgumentOutOfRangeException was unhandled
  Message="Specified argument was out of the range of valid values.\r\nParameter name: index"
  Source="PresentationCore"
  ParamName="index"
  StackTrace:
       at System.Windows.Media.VisualCollection.get_Item(Int32 index)
       at System.Windows.Controls.UIElementCollection.get_Item(Int32 index)
       at System.Windows.Controls.UIElementCollection.System.Collections.IList.get_Item(Int32 index)
       at Microsoft.Windows.Controls.DataGridCellsPanel.ArrangeOverride(Size arrangeSize)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at MS.Internal.Helper.ArrangeElementWithSingleChild(UIElement element, Size arrangeSize)
       at System.Windows.Controls.ItemsPresenter.ArrangeOverride(Size arrangeSize)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at System.Windows.Controls.Grid.ArrangeOverride(Size arrangeSize)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at Microsoft.Windows.Controls.Primitives.DataGridColumnHeadersPresenter.ArrangeOverride(Size finalSize)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at System.Windows.Controls.Grid.ArrangeOverride(Size arrangeSize)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at System.Windows.Controls.Control.ArrangeOverride(Size arrangeBounds)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at System.Windows.Controls.Border.ArrangeOverride(Size finalSize)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at System.Windows.Controls.Control.ArrangeOverride(Size arrangeBounds)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at System.Windows.Controls.Grid.ArrangeOverride(Size arrangeSize)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at MS.Internal.Helper.ArrangeElementWithSingleChild(UIElement element, Size arrangeSize)
       at System.Windows.Controls.ContentPresenter.ArrangeOverride(Size arrangeSize)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at System.Windows.Controls.Border.ArrangeOverride(Size finalSize)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at System.Windows.Controls.Control.ArrangeOverride(Size arrangeBounds)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at System.Windows.Controls.Grid.ArrangeOverride(Size arrangeSize)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at System.Windows.Controls.Grid.ArrangeOverride(Size arrangeSize)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at System.Windows.Controls.Border.ArrangeOverride(Size finalSize)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at System.Windows.Controls.Grid.ArrangeOverride(Size arrangeSize)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at System.Windows.ContextLayoutManager.UpdateLayout()
       at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
       at System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork()
       at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
       at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
       at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
       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)
       at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
       at System.Windows.Threading.DispatcherOperation.InvokeImpl()
       at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
       at System.Threading.ExecutionContext.runTryCode(Object userData)
       at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Windows.Threading.DispatcherOperation.Invoke()
       at System.Windows.Threading.Dispatcher.ProcessQueue()
       at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       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)
       at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)
       at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.Run()
       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 Pcis.Emr.Main.App.Main() in C:\Projects\PCIS\EMRv0.2\Pcis.Emr\Pcis.Emr.Main\obj\Debug\App.g.cs:line 0
  InnerException:

Apr 2, 2009 at 12:07 PM
I have also come across a similar problem when setting DataGridColumn.DisplayIndex on runtime. It seemed only happening for some certain column orders (haven't really worked out the pattern yet). Here is the big stacktrace when the error occured and the application then crashed out. Would this be a bug? Thanks.

 

 

---- DEBUG ASSERTION FAILED ----

---- Assert Short Message ----

Element count from blocks can't be less than total children count

---- Assert Long Message ----

 

at DataGridCellsPanel.GetRealizedChildrenNotInBlockList(List`1 blockList, IList children)

at DataGridCellsPanel.ArrangeOverride(Size arrangeSize)

at FrameworkElement.ArrangeCore(Rect finalRect)

at UIElement.Arrange(Rect finalRect)

at Helper.ArrangeElementWithSingleChild(UIElement element, Size arrangeSize)

at ItemsPresenter.ArrangeOverride(Size arrangeSize)

at FrameworkElement.ArrangeCore(Rect finalRect)

at UIElement.Arrange(Rect finalRect)

at Control.ArrangeOverride(Size arrangeBounds)

at DataGridCellsPresenter.ArrangeOverride(Size finalSize)

at FrameworkElement.ArrangeCore(Rect finalRect)

at UIElement.Arrange(Rect finalRect)

at Grid.ArrangeOverride(Size arrangeSize)

at FrameworkElement.ArrangeCore(Rect finalRect)

at UIElement.Arrange(Rect finalRect)

at Border.ArrangeOverride(Size finalSize)

at FrameworkElement.ArrangeCore(Rect finalRect)

at UIElement.Arrange(Rect finalRect)

at Control.ArrangeOverride(Size arrangeBounds)

at DataGridRow.ArrangeOverride(Size arrangeBounds)

at FrameworkElement.ArrangeCore(Rect finalRect)

at UIElement.Arrange(Rect finalRect)

at VirtualizingStackPanel.ArrangeOverride(Size arrangeSize)

at FrameworkElement.ArrangeCore(Rect finalRect)

at UIElement.Arrange(Rect finalRect)

at Helper.ArrangeElementWithSingleChild(UIElement element, Size arrangeSize)

at ItemsPresenter.ArrangeOverride(Size arrangeSize)

at FrameworkElement.ArrangeCore(Rect finalRect)

at UIElement.Arrange(Rect finalRect)

at ScrollContentPresenter.ArrangeOverride(Size arrangeSize)

at FrameworkElement.ArrangeCore(Rect finalRect)

at UIElement.Arrange(Rect finalRect)

at Grid.ArrangeOverride(Size arrangeSize)

at FrameworkElement.ArrangeCore(Rect finalRect)

at UIElement.Arrange(Rect finalRect)

at Control.ArrangeOverride(Size arrangeBounds)

at FrameworkElement.ArrangeCore(Rect finalRect)

at UIElement.Arrange(Rect finalRect)

at Border.ArrangeOverride(Size finalSize)

at FrameworkElement.ArrangeCore(Rect finalRect)

at UIElement.Arrange(Rect finalRect)

at Control.ArrangeOverride(Size arrangeBounds)

at FrameworkElement.ArrangeCore(Rect finalRect)

at UIElement.Arrange(Rect finalRect)

at Grid.ArrangeOverride(Size arrangeSize)

at FrameworkElement.ArrangeCore(Rect finalRect)

at UIElement.Arrange(Rect finalRect)

at DockPanel.ArrangeOverride(Size arrangeSize)

at FrameworkElement.ArrangeCore(Rect finalRect)

at UIElement.Arrange(Rect finalRect)

at Helper.ArrangeElementWithSingleChild(UIElement element, Size arrangeSize)

at ContentPresenter.ArrangeOverride(Size arrangeSize)

at FrameworkElement.ArrangeCore(Rect finalRect)

at UIElement.Arrange(Rect finalRect)

at Border.ArrangeOverride(Size finalSize)

at FrameworkElement.ArrangeCore(Rect finalRect)

at UIElement.Arrange(Rect finalRect)

at Control.ArrangeOverride(Size arrangeBounds)

at FrameworkElement.ArrangeCore(Rect finalRect)

at UIElement.Arrange(Rect finalRect)

at Helper.ArrangeElementWithSingleChild(UIElement element, Size arrangeSize)

at ContentPresenter.ArrangeOverride(Size arrangeSize)

at FrameworkElement.ArrangeCore(Rect finalRect)

at UIElement.Arrange(Rect finalRect)

at Decorator.ArrangeOverride(Size arrangeSize)

at AdornerDecorator.ArrangeOverride(Size finalSize)

at FrameworkElement.ArrangeCore(Rect finalRect)

at UIElement.Arrange(Rect finalRect)

at Border.ArrangeOverride(Size finalSize)

at FrameworkElement.ArrangeCore(Rect finalRect)

at UIElement.Arrange(Rect finalRect)

at Window.ArrangeOverride(Size arrangeBounds)

at FrameworkElement.ArrangeCore(Rect finalRect)

at UIElement.Arrange(Rect finalRect)

at HwndSource.SetLayoutSize()

at HwndSource.set_RootVisualInternal(Visual value)

at HwndSource.set_RootVisual(Visual value)

at Window.SetRootVisual()

at Window.SetupInitialState(Double requestedTop, Double requestedLeft, Double requestedWidth, Double requestedHeight)

at Window.CreateSourceWindowImpl()

at Window.SafeCreateWindow()

at Window.ShowHelper(Object booleanBox)

at ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)

at ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)

at Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)

at DispatcherOperation.InvokeImpl()

at DispatcherOperation.InvokeInSecurityContext(Object state)

at ExecutionContext.runTryCode(Object userData)

at RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)

at ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)

at ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

at DispatcherOperation.Invoke()

at Dispatcher.ProcessQueue()

at Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)

at HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)

at HwndSubclass.DispatcherCallbackOperation(Object o)

at ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)

at ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)

at Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)

at Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)

at Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)

at HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)

at UnsafeNativeMethods.DispatchMessage(MSG& msg)

at Dispatcher.PushFrameImpl(DispatcherFrame frame)

at Dispatcher.PushFrame(DispatcherFrame frame)

at Dispatcher.Run()

at Application.RunDispatcher(Object ignore)

at Application.RunInternal(Window window)

at Application.Run(Window window)

at Application.Run()

at App.Main() C:\DevelopmentSVN\NET\Source\GUI\Applications\MarketDataViewer\TFS.GUI.Applications.MarketDataViewer.StandaloneApplication\obj\Debug\App.g.cs

at AppDomain._nExecuteAssembly(Assembly assembly, String[] args)

at AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)

at HostProc.RunUsersAssembly()

at ThreadHelper.ThreadStart_Context(Object state)

at ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

at ThreadHelper.ThreadStart()

 

 

 

Aug 12, 2009 at 2:09 PM

I am also having this same issue with the DataGrid when setting the Column DisplayOrders and Width in code.

 

Has anyone managed to resolve this? Or even narrowed down the cause?

Aug 12, 2009 at 3:49 PM

I was able to create a work around and still be able to set the display order by doing the following, inside the loaded event, in the AllergyList.RestoreColumnState is where I set the display index

 

if (!this.loaded)
        {
          this.restoreColumnsTimer = new System.Windows.Threading.DispatcherTimer();
          this.restoreColumnsTimer.Interval = TimeSpan.FromMilliseconds(1);

          this.restoreColumnsTimer.Tick +=
            delegate(object sndr, EventArgs args)
            {
              this.AllergyList.RestoreColumnState(DataCache.ColumnGuid, ListViewId.Allergies);
              this.restoreColumnsTimer.Stop();
            };

    this.restoreColumnsTimer.Start();
}

 

 

Aug 12, 2009 at 3:58 PM

Thanks masontwo

 

Turns out the problem has been fixed in the latest release!

 

http://wpf.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=29117

 

  • DataGridColumn DisplayIndex ArgumentOutOfRange exception (as reported here)

     

  • Sep 11, 2009 at 11:52 AM

    I'm having the same probleme even with the new version!

     

    The problem in my case is that I randomly add new columns and reorder them. So the loaded event will not fire (as it is already loaded).

    My workaround for this problem is to call UpdateLayout() between adding new columns and changing the display index.