【问题标题】:Binding Two Database Tables to the Same Datagrid将两个数据库表绑定到同一个数据网格
【发布时间】:2011-10-01 20:32:05
【问题描述】:

我想知道如何在同一个 wpf 数据网格中使用来自更多 Sql Compact 数据库的两个不同表。我目前有两个表,Accounts 和 AccountType。 Accounts 中有一个外键将其链接到 AccountType,其中它是类型的 ID 号。我在 Accounts 中还有一个字段可以将其链接到父帐户(本质上是另一个帐户 ID 的外键)。下面是我必须将数据网格绑定到 Accounts 表的 wpf。

<Window.Resources>
    <my:MyAccountantDBDataSet x:Key="myAccountantDBDataSet" />
    <CollectionViewSource x:Key="accountsViewSource" Source="{Binding Path=Accounts, Source={StaticResource myAccountantDBDataSet}}" />
    <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>

我想做的是将类型列绑定到 AccountType 表并显示类型名称,而不是类型 ID。此外,对于父帐户的想法相同,我不想显示 ID 号,而是显示帐户名称(如果它有父帐户)。如何对这两列执行此操作?

【问题讨论】:

  • 等等,这可以通过JOINING 表格轻松实现。你可以阅读JOINs

标签: c# wpf binding datagrid


【解决方案1】:

你能创建一个视图连接数据库中的两个表,然后从 C# 中查询视图吗? 这样,所有处理都在数据库上完成,并且可能比您的应用程序更有效。

【讨论】:

  • 谢谢凯文。我知道关于加入的一切,我只是想知道是否有不同的绑定方式。
猜你喜欢
  • 1970-01-01
  • 2013-03-28
  • 2022-01-03
  • 2013-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多