Index out of range setting display index in code

Nov 21, 2008 at 2:35 PM
 I have found a problem when setting datagrid column display indexes programattically

When trying to save the state of users columns in an app i built using the wpf tookit i found that in some circumstances i would get an index out of range error when ordering columns a certain way

I have included an example

Window x:Class="Window1" xmlns=" xmlns:x= Title="Window1" Height="300" Width="300" xmlns:my="">
    <my:DataGrid Margin="12" Name="DataGrid1">
            <my:DataGridTextColumn Header="Column1" x:Name="dgcColumn1" /> 
            <my:DataGridTextColumn Header="Column2" x:Name="dgcColumn2" />
            <my:DataGridTextColumn Header="Column3" x:Name="dgcColumn3" />
            <my:DataGridTextColumn Header="Column4" x:Name="dgcColumn4" />



    Private Sub Window1_Initialized(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Initialized
        dgcColumn1.DisplayIndex = 2
        dgcColumn2.DisplayIndex = 1
        dgcColumn3.DisplayIndex = 0
    End Sub

System.ArgumentOutOfRangeException was unhandled
  Message="Specified argument was out of the range of valid values.\r\nParameter name: index"
When i only have 3 columns it doesnt error but only column 3 gets displayed which could be the root of the problem


Apr 9, 2009 at 10:56 PM
I've got a similar problem.

I'm setting the GridView's column order & visibility from code:

                    int index = 0;
                    foreach (myObject columnData in Properties.Settings.Default.ColumnData)
                        DataGridColumn column = dataGridView.Columns[index];
                        column.DisplayIndex = columnData.Index;
                        column.Visibility = columnData.Visibility ? Visibility.Visible : Visibility.Hidden;

                        // Set up DataGrid context menu item for this column

If I comment out either the DisplayIndex line or Visibility line it works fine all the time - the correct columns are visible or the columns are in the expected order.

If the columns are in the default order then there's no problem. It's only when there are some invisible columns and some columns are in a different order that the exception gets thrown.

The line is (DataGridCellsPanel.cs, ArrangeOverride, 1362):

                        ArrangeChild(children[childIndex] as UIElement, i, arrangeState);

childIndex is out of range.

This is in the WPF Toolkit March 2009 (Version 3.5.40320.1) (3.5.40320.1)

I'm running on .NET 3.5 SP1 on Windows XP SP3 (fully up to date).

Apr 10, 2009 at 9:53 PM
This is a indeed a bug, and we've filed a bug in our database to track the issue.

You may try working around the issue by calling Dispatcher.BeginInvoke at Background priority within a Loaded event. It appears that the issue does not occur if the order is changed after the page has reached a steady state.