Binding DataGirdComboBoxColumn to DataSet doesn't work

550 views Asked by At

I want to create a simple master-detail view in a datagrid.
I tried to bind a DataGirdComboBoxColumn to a DataSet, but it won't work.

Here's my xaml code:

<DataGrid x:Name="DataGrid1" Grid.Column="0" Grid.Row="0" AlternatingRowBackground="#FFF0F0F0" ItemsSource="{Binding}" Height="420" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="ID" Binding="{Binding AirplaneID}" IsReadOnly="True" MinWidth="30"/>
        <DataGridTextColumn Header="Model" Binding="{Binding Model}" IsReadOnly="True" Width="Auto" MinWidth="160"/>
        <DataGridTextColumn Header="Fuel" Binding="{Binding Fuel}" MinWidth="50" />

        <DataGridComboBoxColumn x:Name="ComboBoxColumnPass" Header="Passenger" DisplayMemberPath="Name"
                                        SelectedValueBinding="{Binding AirplaneID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="AirplaneID"/>   
    </DataGrid.Columns>
    <DataGrid.ContextMenu>
        <ContextMenu>
            <MenuItem Command="ApplicationCommands.New" ToolTip="Fügt eine neue Zeile hinzu"/>
            <MenuItem Command="ApplicationCommands.Delete"  ToolTip="Löscht das ausgewählte Element"/>
        </ContextMenu>
    </DataGrid.ContextMenu>
    <DataGrid.RowDetailsTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" Margin="5">
                <TextBlock FontSize="12" TextWrapping="Wrap" Text="{Binding Detail}" />
            </StackPanel>
        </DataTemplate>
    </DataGrid.RowDetailsTemplate>
</DataGrid>

And here's my C# code:

namespace WpfApplicationAirplaneDB
{
    AirplaneDBDataSet AirplaneDataSet = new AirplaneDBDataSet();
    AirplaneDBDataSetTableAdapters.AirplaneTableAdapter AirTableAdapter = new AirplaneDBDataSetTableAdapters.AirplaneTableAdapter();
    AirplaneDBDataSetTableAdapters.PassengerTableAdapter PassTableAdapter = new AirplaneDBDataSetTableAdapters.PassengerTableAdapter();

    public MainWindow()
    {
        InitializeComponent();

        AirTableAdapter.Fill(AirplaneDataSet.Airplane);
        PassTableAdapter.Fill(AirplaneDataSet.Passenger);

        DataGrid1.ItemsSource = AirplaneDataSet.Airplane.DefaultView;
        ComboBoxColumnPass.ItemsSource = AirplaneDataSet.Passenger.DefaultView;

    }
}

And this is the error:

An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll Additional information: TwoWay- oder OneWayToSource-Bindungen funktionieren nicht mit der schreibgeschützten Eigenschaft "AirplaneID" vom Typ "System.Data.DataRowView".

(sorry for the german language exception)

1

There are 1 answers

3
Tim Pohlmann On BEST ANSWER

It seems like AirplaneID is readonly. Either make it writeable or change the binding to OneWay.