Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "unhandled exception" when clicking on WPF DataGrid?

I am using Visual Studio 2010, WPF with C# 4.0 and when clicking the cell in DataGrid I got the following exception:

An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll  Additional information: A TwoWay or OneWayToSource binding cannot work on the read-only property 'Column2' of type 'VindecoderUI.AcesData'.

Here's the code I have:

    <DataGrid AutoGenerateColumns="False" Height="190" Name="nadaDataGrid" Width="304" FrozenColumnCount="1000" ItemsSource="{Binding Source={StaticResource nadaDataCollection}}" CanUserReorderColumns="False" CanUserResizeColumns="True" CanUserSortColumns="False" AlternatingRowBackground="#3F000000" CanUserResizeRows="False" SelectionMode="Single" SelectionUnit="Cell"
               SelectionChanged="dataGrid1_SelectionChanged" AreRowDetailsFrozen="True" >
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Path=NadaSeries}" Header="Nada Series" />
            <DataGridTextColumn Binding="{Binding Path=NadaBS}" Header="Nada BS" />
            <DataGridTextColumn Binding="{Binding Path=MSRP}" Header="MSRP" />
            <DataGridTextColumn Binding="{Binding Path=GVWR}" Header="GVWR" />
            <DataGridTextColumn Binding="{Binding Path=GCWR}" Header="GCWR" />
        </DataGrid.Columns>
    </DataGrid>

    <Window.Resources>
    <CollectionViewSource x:Key="nadaDataCollection"></CollectionViewSource>
    <CollectionViewSource x:Key="acesDataCollection"></CollectionViewSource>
</Window.Resources>
like image 850
Roman Kagan Avatar asked Aug 18 '11 19:08

Roman Kagan


1 Answers

By default, DataGrid cells are editable (which means the bindings are TwoWay.) Since you are binding a collection that includes items with a read-only property, you get an exception when you click on the cell, since editing couldn't work.

You should be able to work around that by adding Mode=OneWay to the column binding for that field.

like image 67
dlev Avatar answered Oct 23 '22 13:10

dlev