views:

290

answers:

2

I have my data model in my ASP.NET website and what i'm attempting to do is retrieve tbl_company records and bind these to a datagrid in silverlight - pretty simple.

I set up my database so tbl_company has a foreign key (company_scope_id) that is linked to tbl_company_scope. From tbl_company_scope I basically want to show the column "scope". My code in silverlight is below:

    <data:DataGrid x:Name="dgCompanies" Grid.Row="0" AutoGenerateColumns="False">
        <data:DataGrid.Columns>
            <data:DataGridTemplateColumn>
                <data:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding name, Mode=TwoWay}"></TextBlock>
                    </DataTemplate>
                </data:DataGridTemplateColumn.CellTemplate>

            </data:DataGridTemplateColumn>
            <data:DataGridTemplateColumn>

                <data:DataGridTemplateColumn.CellTemplate >
                    <DataTemplate>
                        <TextBlock Text="{Binding CompanyScope.scope}"></TextBlock>
                    </DataTemplate>
                </data:DataGridTemplateColumn.CellTemplate>

            </data:DataGridTemplateColumn>
        </data:DataGrid.Columns>

    </data:DataGrid>

(all the naming etc etc is correct but basically "scope" will not show when I bind to the datagrids ItemSource)

Does anyone know how you're ment to show the scope column in the datagrid?

Cheers

A: 

I did a quick test of "dot" binding in the DataGrid and it worked as expected. I basically took your Xaml and wired up some POCO data classes. Here's the code:

<data:DataGrid x:Name="dgCompanies" Grid.Row="0" AutoGenerateColumns="False">
        <data:DataGrid.Columns>
            <data:DataGridTemplateColumn>
                <data:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Name, Mode=TwoWay}"></TextBlock>
                    </DataTemplate>
                </data:DataGridTemplateColumn.CellTemplate>

            </data:DataGridTemplateColumn>
            <data:DataGridTemplateColumn>

                <data:DataGridTemplateColumn.CellTemplate >
                    <DataTemplate>
                        <TextBlock Text="{Binding CompanyScope.Scope}"></TextBlock>
                    </DataTemplate>
                </data:DataGridTemplateColumn.CellTemplate>

            </data:DataGridTemplateColumn>
        </data:DataGrid.Columns>
    </data:DataGrid>

The data classes:

public class CompanyScope
    {
        public string Scope { get; set; }
    }

    public class Company
    {
        public string Name { get; set; }
        public CompanyScope CompanyScope { get; set; }
    }

And finally, the code behind the the constructor:

ObservableCollection<Company> companies = new ObservableCollection<Company>
            {
                new Company { Name="Company One", CompanyScope=new CompanyScope { Scope="Some Scope" }},
                new Company { Name="Company Two", CompanyScope=new CompanyScope { Scope="Some Other Scope" }},
                new Company { Name="Company Three", CompanyScope=new CompanyScope { Scope="More Scope" }}
            };

            dgCompanies.ItemsSource = companies;

I got the expected result of 2 columns in the grid, one showing company name and the other showing its scope. Are you sure your Scope objects are loaded too - maybe you loaded Companies but not their Scopes.

James Cadd
A: 

I just tested your code and you're right it should work like you say, thanks for the reply, I guess I need to go and learn more about ADO.NET EF.

Cheers again.