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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With