如何用关系绑定两个数据表

时间:2013-04-15 09:38:03

标签: wpf data-binding datatable

我在数据集和关联中有两个数据表

DataSet newDataset = new DataSet("dsShop");
DataTable customers = new DataTable("customers");
DataTable orders = new DataTable("orders");
customers.Columns.Add("customerid", typeof(int));
customers.Columns.Add("title", typeof(string));
customers.Columns.Add("phone", typeof(int));
customers.Rows.Add(new object[] { 1, "Mr.Smith", 3442343 });
customers.Rows.Add(new object[] { 2, "Mr.Klein", 3442343 });
orders.Columns.Add("id", typeof(int));
orders.Columns.Add("customerid", typeof(int));
orders.Columns.Add("caption", typeof(string));
orders.Columns.Add("order", typeof(int));
orders.Rows.Add(new object[] { 1,1,"dildo",1 });
orders.Rows.Add(new object[] { 3, 2, "shorts", 1 });
orders.Rows.Add(new object[] { 4, 3, "umbrella", 1 });
newDataset.Tables.Add(customers);
newDataset.Tables.Add(orders);
newDataset.Relations.Add(new DataRelation("order_customerfk", customers.Columns[""customerid""], orders.Columns["customerid"]));

现在我想为“客户”制作编辑窗口。 customers.Columns显示在textbox和orders.Columns中。在datagrid中显示。

我做了类似这样的事情但是id不起作用

public CustomerEdit(int customerID)
{
    var curCustomer = dsShop.customer.Select("customerid='" + customerID + "'");  
    gCustomer.DataContext = curCustomer.FirstOrDefault(); 
}


<Grid x:Name="gCustomer">
<Grid.RowDefinitions>
    <RowDefinition Height="*"/>
    <RowDefinition Height="*"/>
    <RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
    <ColumnDefinition Width="*"/>
    <ColumnDefinition Width="2*"/>
    <ColumnDefinition Width="*"/>
    <ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>

<Label Content=""customer id"" Style="{StaticResource g_PropertyLabel}" Grid.Row="0" Grid.Column="0"/>
<TextBox x:Name="tbx"Customerid"" Text="{Binding Path=customerid, Mode=TwoWay}" Style="{StaticResource g_PropertyTextBox}" Grid.Row="0" Grid.Column="1"/>
<Label Content="title" Style="{StaticResource g_PropertyLabel}" Grid.Row="1" Grid.Column="0"/>
<TextBox x:Name="tbxTitle" Text="{Binding Path=title, Mode=TwoWay}" Style="{StaticResource g_PropertyTextBox}" Grid.Row="1" Grid.Column="1"/>
<Label Content="phone" Style="{StaticResource g_PropertyLabel}" Grid.Row="2" Grid.Column="0"/>
<TextBox x:Name="tbxPhone" Text="{Binding Path=phone, Mode=TwoWay}" Style="{StaticResource g_PropertyTextBox}" Grid.Row="2" Grid.Column="1"/>

<DataGrid x:Name="dgOrders" IsSynchronizedWithCurrentItem="True" AutoGenerateColumns="False" ItemsSource="{Binding Path=order_customerfk}"  CanUserAddRows="True" Grid.Row="5" Grid.Column="1" Grid.ColumnSpan="3">
    <DataGrid.Columns>
        <DataGridTextColumn Header="ID" Binding="{Binding Path=id, Mode=TwoWay}" Width="50"/>
        <DataGridTextColumn Header="Caption" Binding="{Binding Path=caption, Mode=TwoWay}" Width="250"/>
        <DataGridTextColumn Header="Order" Binding="{Binding Path=order, Mode=TwoWay}" Width="50"/>
    </DataGrid.Columns>
</DataGrid>
</Grid>

在此窗口中我想编辑客户数据和订单数据。 客户我可以,但订单不能......

0 个答案:

没有答案