locked
Basic master-detail with EF and Datagrid RRS feed

  • Question

  • Hi all,

    Feeling like an idiot atm.  How do I create an 'Order Form' that shows Order data on the main part of the form, and order details in the datagrid, in terms of the CollectionSource, ItemsSource, RelatedItem etc etc.

    My application is database first, I have an Order table with a one to many relationship to the OrderDetail table.  I have an OrderCollectionSource.xaml:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006
         xmlns:model="clr-namespace:MyModel"
         mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008">

      <CollectionViewSource x:Key="OrdersViewSource" d:DesignSource="{d:DesignInstance model:Order, CreateList=True}" />
      <CollectionViewSource x:Key="OrderDetailsViewSource" d:DesignSource="{d:DesignInstance model:OrderDetail, CreateList=True}" />
    </ResourceDictionary>

    Xaml for the main form's resources:

    <Window.Resources>
        <ResourceDictionary>
          <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="FormStyles.xaml" />
            <ResourceDictionary Source="OrderCollectionSource.xaml" />
       </ResourceDictionary.MergedDictionaries>
     </ResourceDictionary>

    The main part of the form is made up of textboxes, labels, checkboxes etc. and all are working.  Typical xaml code:

    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" DataContext="{StaticResource OrdersViewSource}">
          <Label Content="ID:" Name="lblOrderID" FontSize="12"  />
          <TextBlock  Name="txtOrderID" MinWidth="20" Text="{Binding Path=OrderID, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" />
          <Label Content="Order Date:" Name="lblOrderDate" FontSize="14"  />
          <TextBlock  Name="txtOrderDate" MinWidth="100" Text="{Binding Path=OrderDate, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" />
    </StackPanel>

    Now I want to add a DataGrid showing the details of the current order, and I'm stuck on the syntax.  I have tried the following:

    <DataGrid x:Name="dgridOrderDetail" DataContext="{StaticResource OrdersViewSource}" ItemsSource="{Binding Path=OrderDetail}"/> 

    (I also tried < ... Path=OrderDetails}"/> as I wasn't sure if EF would pluralize the table name.)  This didn't work at all, however in VS, I did notice that if I left the attribute as 'ItemsSource="{Binding}"/>, then the preview window showed auto-generated columns for the parent (Order) table.

    I feel I'm doing something stupidly wrong, as I have previously had no trouble bringing child data into a form, except I've only done it through scalar controls like TextBox, not list controls like DataGrid. 

    So, should I be using the parent OrdersViewSource and trying to navigate to the OrderDetails, or should my DataGrid's DataContext be set to the OrderDetailViewSource, and if so, how do I only show the OrderDetails relating to the current Order being displayed in the main part of the form?

    If someone could show me or point me to the correct syntax it would be most appreciated.

    cheers


    • Edited by nob0dy Thursday, May 29, 2014 8:02 AM
    Thursday, May 29, 2014 7:48 AM

Answers

  • Provided that you have set the Source property of the OrderDetailsViewSource to a collection of the OrderDetail entity objects that you want to display in the DataGrid, you could bind the ItemsSource property of the DataGrid like this:

    <DataGrid x:Name="dgridOrderDetail" ItemsSource="{Binding Source={StaticResource OrderDetailsViewSource}}"/>
    

    • Marked as answer by nob0dy Friday, May 30, 2014 3:33 AM
    Thursday, May 29, 2014 9:54 AM

All replies

  • Provided that you have set the Source property of the OrderDetailsViewSource to a collection of the OrderDetail entity objects that you want to display in the DataGrid, you could bind the ItemsSource property of the DataGrid like this:

    <DataGrid x:Name="dgridOrderDetail" ItemsSource="{Binding Source={StaticResource OrderDetailsViewSource}}"/>
    

    • Marked as answer by nob0dy Friday, May 30, 2014 3:33 AM
    Thursday, May 29, 2014 9:54 AM
  • Allow me to apologise.

    I said I was feeling like an idiot.  This is the feeling you get when you link your detail table to your parent via the detail table's **primary key**.  EF thought I had a 1-[0|1] relationship not 1-m.  Marking your post as the answer as it showed me what I had done wrong.

    Thanks and sorry.

    Friday, May 30, 2014 3:32 AM