开发者

Binding Two Database Tables to the Same Datagrid

I would like to know how I would be able to use two different tables from more Sql Compact database in the same wpf datagrid. I have two tables currently, Accounts and AccountType. There is a foreign key in Accounts to link it to the AccountType in which it is the ID number of the type. I also have a field in Accounts to link it to a parent account (essentially a foreign key to another account's id). Below is the wpf I have to bind the datagrid to the Accounts table.

<Window.Resources>
    <my:MyAccountantDBDataSet x:Key="myAccountantDBDataSet" />
    <CollectionViewSource x:Key="accountsViewSource" Source="{Binding Path=Accounts, Source={StaticResource myAccountantDBDataSet}}" />
    &开发者_如何学编程lt;CollectionViewSource x:Key="accountTypeViewSource" Source="{Binding Path=AccountType, Source={StaticResource myAccountantDBDataSet}}" />
</Window.Resources>

            <DataGrid Background="DimGray" Name="GridAccounts" CanUserAddRows="True" CanUserDeleteRows="True" ItemsSource="{Binding Source={StaticResource accountsViewSource}}" AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Name" Binding="{Binding Path=AccountName}"/>
                    <DataGridTextColumn Header="Description" Binding="{Binding Path=AccountDesc}" />
                    <DataGridTextColumn Header="Number" Binding="{Binding Path=AccountNumber}"/>
                    <DataGridTextColumn Header="Type" />
                    <DataGridTextColumn Header="Parent Account" />
                </DataGrid.Columns>
            </DataGrid>

What I would like to do, is bind the Type column to the AccountType table and show the Type Name, not the Type id. Also, same idea for the Parent Account, I don't want to show the id number, but rather the Account Name if it has a parent account. How can I do this to these two columns?


Can you create a view joining the two tables in the database and then query the view from C#? This way all the processing is done on the database and could be more efficient than from your application.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜