WPF DataGrid binding nested DataRow column values

Apr 19, 2010 at 1:25 AM

Hi,
Every binding example I've seen on the net uses Collections of custom entity classes so that if an Item has a property which itself is another Item, you can access the nested item's properties by using a Binding Path expression something like Parent.PropertyA, where Parent is a property of Item. This doesn't seem to be the case with DataRow, as you can see in the sample code below. How can I make the first column show the value 'r1c1' for all rows? Also, as you can see, I've tried many different Path expressions and recorded the result of each in a comment block in Window1.xaml.

Window1.xaml:
<Window
    x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Data="clr-namespace:System.Data;assembly=System.Data"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:WpfToolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="Window1"
    Height="300"
    Width="300">
    
    <Window.Resources>
        <local:DataRowConverter x:Key="drc" />
    </Window.Resources>
    
    <Grid>
        <WpfToolkit:DataGrid
            Name="dgSampleData"
            ItemsSource="{Binding}"
            AutoGenerateColumns="True"
            Margin="0,75,0,0">
            <WpfToolkit:DataGrid.Columns>
                <WpfToolkit:DataGridTextColumn
                    Header="Bound Data" Binding="{Binding Path=(Data:DataRow)Col3.Col1}"
                    
                    />
                    <!--
                    
                    Binding="{Binding [Col3]}"                  = "System.Data.DataRow"
                    Binding="{Binding Col3}"                    = "System.Data.DataRow"
                    Binding="{Binding Col3[Col1]}"              = ""
                    Binding="{Binding Col3.Item[Col1]}"         = ""
                    Binding="{Binding Col3[0]}"                 = Error: A TwoWay or OneWayToSource binding cannot work on the read-only property 'Chars' of type 'System.String'.
                    Binding Path=Col3[0], Mode=OneWay           = "S"
                    Binding Path=Col3[4], Mode=OneWay           = "e"
                    Binding Path=/Col3[0]                       = ""
                    Binding Path=/Col3                          = ""
                    Binding Path=/Item[0]                       = ""
                    Binding Path=Col3.toString(), Mode=OneWay   = ""
                    Binding (Rows)[0][Col1]                     = ""
                    Binding (Rows)[0][Col1]
                    Binding Path=Col3.Col1                      = ""
                    
                
                    -->
            </WpfToolkit:DataGrid.Columns>
            
        </WpfToolkit:DataGrid>
        <Button Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="Button1" VerticalAlignment="Top" Width="75">Button</Button>
    </Grid>
</Window>

Window1.xaml.vb:
Imports System.Data

Class Window1

    Private dtSampleData As New DataTable

    Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded

        dtSampleData.Columns.Add("Col1")
        dtSampleData.Columns.Add("Col2")
        dtSampleData.Columns.Add("Col3")
        dtSampleData.Rows.Add(dtSampleData.NewRow())
        dtSampleData.Rows.Add(dtSampleData.NewRow())
        dtSampleData.Rows(0).Item(0) = "r1c1"
        dtSampleData.Rows(0).Item(1) = "r1c2"
        dtSampleData.Rows(0).Item(2) = dtSampleData.Rows(0)
        dtSampleData.Rows(1).Item(0) = "r2c1"
        dtSampleData.Rows(1).Item(1) = "r2c2"
        dtSampleData.Rows(1).Item(2) = dtSampleData.Rows(0)
        dgSampleData.DataContext = dtSampleData

        Dim dgcCol1 As Microsoft.Windows.Controls.DataGridTextColumn

        dgcCol1 = dgSampleData.Columns(0)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click

    End Sub
End Class

Thanks in advance,
Dave