Windows Vista Forums

Binding to a DataSet and related rows
  1. #1


    jmagaram Guest

    Binding to a DataSet and related rows

    I have a DataSet filled with two tables:

    Person [PersonID,First,Last]
    PhoneNumber [PersonID,PhoneNumber,PhoneType]

    I want to display a tree of each person and their list of phone numbers. The
    top level is easy - I bind to a DataView on the Person table. But I can't
    figure out how to also show the associated phone numbers (as a DataView so it
    gets updated automatically). One idea I had was to create a Method on the
    DataSet something like this:



    DataView GetPhoneNumbers(DataRow person)

    I don't know how to refer to this method in the ItemsSource in the XAML.
    Binding to properties seems straightforward, but not to methods. The
    templates look something like this:

    <DataTemplate x:Key="PhoneRowTemplate">
    <StackPanel Orientation="Horizontal">
    <TextBlock Text="{Binding Path=PhoneType}"/>
    <TextBlock Text="{Binding Path=PhoneNumber}"/>
    </StackPanel>
    </DataTemplate>

    <DataTemplate x:Key="PersonRowTemplate">
    <StackPanel Orientation="Vertical">
    <TextBlock Text="{Binding Path=First}"/>
    <TextBlock Text="{Binding Path=Last}"/>
    <ListBox ItemsSource="?????" ItemTemplate="{StaticResource
    PhoneRowTemplate}"></ListBox>
    </StackPanel>
    </DataTemplate>

      My System SpecsSystem Spec

  2. #2


    jmagaram Guest

    RE: Binding to a DataSet and related rows

    I implemented a major hack to solve this. For each row in the parent table, I
    created a column of type DataView called RelatedPhoneNumbers and filled it
    with a custom built DataView. Each time a row is added to the parent table,
    the TableNewRow event fills in this special column automatically. Then I just
    set the ItemsSource property to "RelatedPhoneNumbers". There must be a better
    way.

    Ideas anyone?

    "jmagaram" wrote:

    > I have a DataSet filled with two tables:
    >
    > Person [PersonID,First,Last]
    > PhoneNumber [PersonID,PhoneNumber,PhoneType]
    >
    > I want to display a tree of each person and their list of phone numbers. The
    > top level is easy - I bind to a DataView on the Person table. But I can't
    > figure out how to also show the associated phone numbers (as a DataView so it
    > gets updated automatically). One idea I had was to create a Method on the
    > DataSet something like this:
    >
    > DataView GetPhoneNumbers(DataRow person)
    >
    > I don't know how to refer to this method in the ItemsSource in the XAML.
    > Binding to properties seems straightforward, but not to methods. The
    > templates look something like this:
    >
    > <DataTemplate x:Key="PhoneRowTemplate">
    > <StackPanel Orientation="Horizontal">
    > <TextBlock Text="{Binding Path=PhoneType}"/>
    > <TextBlock Text="{Binding Path=PhoneNumber}"/>
    > </StackPanel>
    > </DataTemplate>
    >
    > <DataTemplate x:Key="PersonRowTemplate">
    > <StackPanel Orientation="Vertical">
    > <TextBlock Text="{Binding Path=First}"/>
    > <TextBlock Text="{Binding Path=Last}"/>
    > <ListBox ItemsSource="?????" ItemTemplate="{StaticResource
    > PhoneRowTemplate}"></ListBox>
    > </StackPanel>
    > </DataTemplate>


      My System SpecsSystem Spec

  3. #3


    Douglas Stockwell Guest

    Re: Binding to a DataSet and related rows

    You could use a Converter (see IValueConverter). This is similar to calling
    your GetPhoneNumbers method.

    <ListBox ItemsSource="{Binding Converter={...}}"
    ItemTemplate="{StaticResource ...

    Assuming you can crawl back up the data structure to get the DataSet from a
    Person row.

    - Doug

    "jmagaram" <jmagaram@discussions.microsoft.com> wrote in message
    news:CB22A3FE-34BD-4991-A195-46675765A8AF@microsoft.com...
    >I have a DataSet filled with two tables:
    >
    > Person [PersonID,First,Last]
    > PhoneNumber [PersonID,PhoneNumber,PhoneType]
    >
    > I want to display a tree of each person and their list of phone numbers.
    > The
    > top level is easy - I bind to a DataView on the Person table. But I can't
    > figure out how to also show the associated phone numbers (as a DataView so
    > it
    > gets updated automatically). One idea I had was to create a Method on the
    > DataSet something like this:
    >
    > DataView GetPhoneNumbers(DataRow person)
    >
    > I don't know how to refer to this method in the ItemsSource in the XAML.
    > Binding to properties seems straightforward, but not to methods. The
    > templates look something like this:
    >
    > <DataTemplate x:Key="PhoneRowTemplate">
    > <StackPanel Orientation="Horizontal">
    > <TextBlock Text="{Binding Path=PhoneType}"/>
    > <TextBlock Text="{Binding Path=PhoneNumber}"/>
    > </StackPanel>
    > </DataTemplate>
    >
    > <DataTemplate x:Key="PersonRowTemplate">
    > <StackPanel Orientation="Vertical">
    > <TextBlock Text="{Binding Path=First}"/>
    > <TextBlock Text="{Binding Path=Last}"/>
    > <ListBox ItemsSource="?????" ItemTemplate="{StaticResource
    > PhoneRowTemplate}"></ListBox>
    > </StackPanel>
    > </DataTemplate>



      My System SpecsSystem Spec

  4. #4


    Bart Mermuys Guest

    Re: Binding to a DataSet and related rows

    Hi,

    "jmagaram" <jmagaram@discussions.microsoft.com> wrote in message
    news:29DEC174-5E49-4876-B333-9E848354452B@microsoft.com...
    >I implemented a major hack to solve this. For each row in the parent table,
    >I
    > created a column of type DataView called RelatedPhoneNumbers and filled it
    > with a custom built DataView. Each time a row is added to the parent
    > table,
    > the TableNewRow event fills in this special column automatically. Then I
    > just
    > set the ItemsSource property to "RelatedPhoneNumbers". There must be a
    > better
    > way.
    >


    If you add a "Person-PhoneNumber" DataRelation to the DataSet, then the
    DataView of the Person table will already have an extra column (with the
    same name as the relation) which returns a child DataView of the PhoneNumber
    table.

    HTH,
    Greetings


    > Ideas anyone?
    >
    > "jmagaram" wrote:
    >
    >> I have a DataSet filled with two tables:
    >>
    >> Person [PersonID,First,Last]
    >> PhoneNumber [PersonID,PhoneNumber,PhoneType]
    >>
    >> I want to display a tree of each person and their list of phone numbers.
    >> The
    >> top level is easy - I bind to a DataView on the Person table. But I can't
    >> figure out how to also show the associated phone numbers (as a DataView
    >> so it
    >> gets updated automatically). One idea I had was to create a Method on the
    >> DataSet something like this:
    >>
    >> DataView GetPhoneNumbers(DataRow person)
    >>
    >> I don't know how to refer to this method in the ItemsSource in the XAML.
    >> Binding to properties seems straightforward, but not to methods. The
    >> templates look something like this:
    >>
    >> <DataTemplate x:Key="PhoneRowTemplate">
    >> <StackPanel Orientation="Horizontal">
    >> <TextBlock Text="{Binding Path=PhoneType}"/>
    >> <TextBlock Text="{Binding Path=PhoneNumber}"/>
    >> </StackPanel>
    >> </DataTemplate>
    >>
    >> <DataTemplate x:Key="PersonRowTemplate">
    >> <StackPanel Orientation="Vertical">
    >> <TextBlock Text="{Binding Path=First}"/>
    >> <TextBlock Text="{Binding Path=Last}"/>
    >> <ListBox ItemsSource="?????" ItemTemplate="{StaticResource
    >> PhoneRowTemplate}"></ListBox>
    >> </StackPanel>
    >> </DataTemplate>




      My System SpecsSystem Spec

Binding to a DataSet and related rows problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Alternate rows colors Jason1008 PowerShell 2 10 Jun 2008
Binding inside binding question Yoavo Avalon 0 03 Dec 2007
Binding to a Foreign Key Binding? -=B3N=- Avalon 0 14 Jun 2007
Binding Question (Binding in General) Jason Avalon 2 09 May 2007
Binding a TreeView to a large dataset. Tom Avalon 1 20 Apr 2006