我想创建一个窗口,让我可以插入一个公司,其中包含我想要的尽可能多的联系人。表之间的关系我看起来像这样: http://imgur.com/xP2M49k
这是我窗口的XAML(Grid和其他一些不必要的细节被省略):
<Label Grid.Row="0" Grid.Column="0" Margin="3 3 3 3">Firmenname</Label>
<TextBox x:Name="UiCompanyNameTextBox" Grid.Row="0" Grid.Column="1" Margin="3 3 3 3" Text="{Binding Path=CompanyName}"/>
<Label Grid.Row="0" Grid.Column="3" Margin="3 3 3 3">Branche</Label>
<ComboBox x:Name="UiBranchComboBox" Grid.Row="0" Grid.Column="4" Margin="3 3 3 3" ItemsSource="{Binding}" DisplayMemberPath="BranchName"
SelectedValuePath="id" SelectedValue="{Binding Path=Branch}"/>
<DataGrid x:Name="UiContactPersonDataGrid" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="5" CanUserAddRows="True" CanUserDeleteRows="True"
Margin="3 3 3 3" ItemsSource="{Binding}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Company}" Visibility="Collapsed"></DataGridTextColumn>
<DataGridComboBoxColumn x:Name="UiPersonDataGridComboBoxColumn" Header="Kontaktperson" DisplayMemberPath="Forename"
SelectedValuePath="id" SelectedValueBinding="{Binding Path=Person}"/>
<DataGridComboBoxColumn x:Name="UiPositionDataGridComboBoxColumn" Header="Position" DisplayMemberPath="Position"
SelectedValuePath="id" SelectedValueBinding="{Binding Path=Position}"/>
</DataGrid.Columns>
</DataGrid>
这是我的代码背后:
public partial class AddCompany : Window
{
private DataRowView companyDataRowView;
private ClientManager.ClientManagerDataSet.ContactPersonDataTable contactPersonDataTable;
public AddCompany()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
UiBranchComboBox.ItemsSource = DataSetManager.BranchTA.GetData();
ClientManagerDataSet.CompanyDataTable companyDataTable = DataSetManager.CompanyTA.GetData();
companyDataRowView = companyDataTable.DefaultView.AddNew();
UiGrid.DataContext = companyDataRowView;
companyDataRowView.BeginEdit();
contactPersonDataTable = new ClientManager.ClientManagerDataSet.ContactPersonDataTable();
DataView dv = contactPersonDataTable.DefaultView;
UiContactPersonDataGrid.DataContext = contactPersonDataTable;
UiPersonDataGridComboBoxColumn.ItemsSource = DataSetManager.PersonTA.GetData();
UiPositionDataGridComboBoxColumn.ItemsSource = DataSetManager.PositionTA.GetData();
}
private void UiApplyButton_Click(object sender, RoutedEventArgs e)
{
companyDataRowView.EndEdit();
DataSetManager.CompanyTA.Update(companyDataRowView.Row);
DataSetManager.ContactPersonTA.Update(contactPersonDataTable);
this.Close();
}
}
公司的插入工作。但是,我无法绑定Datagrid,以便它插入具有新公司的Company-Id的行。 通常我会用一个事务做这个,但是因为使用了DataSet,我认为这必须以某种方式可能吗?