Answer the question
In order to leave comments, you need to log in
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 };
}
<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>
Answer the question
In order to leave comments, you need to log in
Try like this:
<DataGridComboBoxColumn ItemsSource="{Binding Source=Manufacturers}" DisplayMemberPath="name" SelectedValuePath="id" SelectedValueBinding="{Binding manufacturer_id}" Header="Manufacturer" Width="*" />
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question