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

I am using Visual Studio 2010, WPF with C # 4.0 and when I clicked a cell in a 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 is 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> 
+4
source share
1 answer

By default, DataGrid cells are edited (which means TwoWay bindings.) Because you bind a collection that includes items with a read-only property, you get an exception when you click on a cell because editing cannot work.

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

+9
source

All Articles