0

I have a DataGrid and I want the user to be able to show/hide columns on it.

I have a button that will show/hide a specific column, and it works fine on one (fast, graphics card, .NET 4.5) machine but not on a different (slow, no graphics hardware, .NET 4.0) machine.

The InvalidOperationException is thrown when hiding then showing the same column... Note that multiple columns can be turned off (by setting their Visiblity to Collapsed), but as soon as one that is already off is turned back on (setting Visibility to Visible) the application crashes and throws the exception.

Looking at the Event Viewer, the exception is being thrown in the DataGrid's AddLogicalChild method, which calls ChangeLogicalParent (I'm assuming on the column being added- note I cannot debug this with breakpoints as the machine I am running it on does not have Visual Studio installed)

Any ideas on what could be causing the app to throw the exception on one machine but not another? Any chance that the .NET runtime has anything to do with it? (upgrading to .NET 4.5 on the machine running the app may not be feasible)

TranquilMarmot
  • 2,034
  • 2
  • 19
  • 25

2 Answers2

1

i use the following code to show/hide columns

    <DataGrid ItemsSource="{Binding MyView}">
        <DataGrid.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Show/ Hide">
                    <StackPanel>
                        <ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.Columns, Mode=OneWay}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate DataType="{x:Type DataGridColumn}">
                                    <CheckBox Content="{Binding Path=Header, Mode=OneWay}" 
                                              IsChecked="{Binding Path=Visibility, Converter={StaticResource TrueIfVisibleConverter}}"/>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </StackPanel>
                </MenuItem>
            </ContextMenu>
        </DataGrid.ContextMenu>
      </DataGrid>

TrueIfVisibleConverter simply set true to visible and false to collapsed.

blindmeis
  • 22,175
  • 7
  • 55
  • 74
  • I ended up using code from [here](http://stackoverflow.com/questions/17392162/bind-visibility-to-checkable-menu-item-shows-error-service-provider-is-missing) (I only want to be able to show/hide certain columns) and it seems like binding the visibility in XAML rather than setting it in code-behind fixes the exception being thrown. But now I'm running into another issue! I want to bind the source of the converter to a checkbox that resides in a different user control (the grid is in its own user control) but I feel like that may be a different topic altogether. – TranquilMarmot Sep 25 '14 at 16:02
0

Believe it or not... updating the .NET version from 4.0 to 4.5 fixed the problem!

TranquilMarmot
  • 2,034
  • 2
  • 19
  • 25