C
C
cheerfulatlas2013-11-29 19:38:10
WPF
cheerfulatlas, 2013-11-29 19:38:10

Using Combobox in WPF DataGrid with foreign key relationship?

Hello.
There was a task to write application for filling of seven tables of a relational DB.
MSSQL, WPF, Entity Framework were chosen as tools. The article
was taken as an example . I removed the search functionality and grouping, created an interface for seven tables (seven DataGrids), added collections to the ViewModel. For example, two tables will suffice.

private launchesEntities _dataContext;

        public ObservableCollection<spacecraft> Spacecrafts { get; private set; }
        public ObservableCollection<manufacturer> Manufacturers { get; private set; }

        public MainWindowViewModel()
        {
            _dataContext = new launchesEntities();            
           
            Manufacturers = new ObservableCollection<manufacturer>(_dataContext.manufacturers);
            Spacecrafts = new ObservableCollection<spacecraft>(_dataContext.spacecrafts);
        
            Save = new ActionCommand(SaveChanges) { IsExecutable = true };            
        }

Some interface:
<DataGrid ItemsSource="{Binding Manufacturers}"
              AutoGenerateColumns="False"
              Grid.Row="4" Grid.Column="1" Margin="5"
              RowDetailsVisibilityMode="VisibleWhenSelected">
      <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding name}" Header="Name" Width="3*"/>
        <DataGridTextColumn Binding="{Binding country}" Header="Country" Width="2*"/>
      </DataGrid.Columns>
</DataGrid>
<DataGrid ItemsSource="{Binding Spacecrafts}"
              AutoGenerateColumns="False"
              Grid.Row="8" Grid.Column="1" Margin="5"
              RowDetailsVisibilityMode="VisibleWhenSelected">
      <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding name}" Header="Name" Width="3*"/>
        <!-- Здесь необходим Combobox -->
      </DataGrid.Columns>
</DataGrid>

Now I need to place in the second column of the DataGrid related to Spacecrafts, drop-down lists that allow you to select a manufacturer, and you need to use a foreign key relationship, and show the name field, and change the manufacturer_id field.
EF Entity Model:
y5ij.png
I've found several examples to solve a similar problem, but my limited fundamental knowledge is not enough to use in the current context, XAML is not a fairly obvious tool.
Thanks for the help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
EasyX, 2013-11-29
@cheerfulatlas

Try like this:

<DataGridComboBoxColumn ItemsSource="{Binding Source=Manufacturers}" DisplayMemberPath="name" SelectedValuePath="id" SelectedValueBinding="{Binding manufacturer_id}" Header="Manufacturer" Width="*" />

E
EasyX, 2013-12-02
@EasyX

Your version is a variation on the theme of the answer I suggested, but instead of DataGridComboBoxColumn, it's a template with a ComboBox.
Tell me, the last option did not work?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question