wpf 如何将数据从2个表导入到数据网格(使用外键连接的表)

tf7tbtn2  于 2023-02-05  发布在  其他
关注(0)|答案(1)|浏览(151)

我的2个表有2个类(User_Tbl.cs和RegFee_Tbl.cs),我的类通过外键连接在一起(上图),下面是(下图)RegFee_Tbl.sc和User_Tbl.cs的属性

这是我的数据网格(memebersDatagride是数据网格的名称),我绑定了此数据网格的3列用于从RegFee_Tbl. cs导入数据,我绑定了此数据网格的1列用于从User_Tbl.cs导入数据,并且在我的mainWindows.cs中,我使用此方法将项目从数据库加载到我的数据网格,但我不知道如何将数据从2个不同的表(用FK连接在一起的表)导入到一个datagrid,这是我从表(MainWindows.cs)加载项的方法:

public partial class Dashboard : UserControl
    {
        GymDbEntities db = new GymDbEntities();
        public static DataGrid datagrid;
        public Dashboard()
        {
        InitializeComponent();

        Load();

    }

    private void Load()
    {

        membersdataGrid.ItemsSource = db.User_Tbl.ToList();
        //membersdataGrid.ItemsSource = db.RegFee_Tbl.ToList();
        datagrid = membersdataGrid;
    }

这是我用xaml写的数据网格

<DataGrid Language="fa-IR" x:Name="membersdataGrid" RowStyle="{DynamicResource DataGridRowStyle1}" ColumnHeaderStyle="{DynamicResource DataGridColumnHeaderStyle1}" CellStyle="{DynamicResource DataGridCellStyle1}" Style="{DynamicResource DataGridStyle1}">
            <DataGrid.Columns>
                <!--bara eyjad sotone checkbox-->
                <DataGridCheckBoxColumn CanUserResize="False" Width="auto" ElementStyle="{StaticResource CheckBoxStyle1}">
                    <DataGridCheckBoxColumn.HeaderTemplate>
                        <DataTemplate>
                            <CheckBox Style="{StaticResource CheckBoxStyle1}" />
                        </DataTemplate>
                    </DataGridCheckBoxColumn.HeaderTemplate>
                </DataGridCheckBoxColumn>

                <DataGridTextColumn Header="#" IsReadOnly="True" CanUserReorder="False" Width="auto" Binding="{Binding ID}"/>

                <DataGridTemplateColumn Header="اعضا" IsReadOnly="True" Width="*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <Border Width="25" Height="25" CornerRadius="50" Margin="0 0 10 0" Background="{Binding BgColor}">
                                    <TextBlock Text="{Binding Character}" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="White" Margin="0 0 0 1"/>
                                </Border>
                                <TextBlock Text="{Binding Name}"  VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

                    <DataGridTextColumn  Header="نام پدر" IsReadOnly="True" Width="*" Binding="{Binding Fee}"/>
                    <DataGridTextColumn  Header="شماره تماس" IsReadOnly="True" Width="*" Binding="{Binding NumberPhone}"/>
                    <DataGridTextColumn  Header="آخرین واریزی" IsReadOnly="True" Width="*" Binding="{Binding FatherName}" />

                <DataGridTemplateColumn Header="عملیات" IsReadOnly="True" Width="auto">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                    <Button  x:Name="GridEditBtn" Style="{StaticResource gridEditbutton}">
                                        <Icon:PackIconMaterial Kind="PencilBoxOutline" Style="{StaticResource gridButtonIcon}"/>
                                </Button>
                                <Button x:Name="GridRemoveBtn" Click="GridRemoveBtn_Click" Style="{StaticResource gridRemovebutton}" Margin="5 0 0 0" >
                                    <Icon:PackIconMaterial Kind="DeleteOutline" Style="{StaticResource gridButtonIcon}" />
                                </Button>
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
qnyhuwrf

qnyhuwrf1#

你使用的是Entity,所以你有一个Context(db),每个表都是一个列表对象,见下面的代码。

GymDbEntities db = new GymDbEntities();

var results = (from u in db.User_Tbl
   join r in db.RegFee_Tbl on u.NavigationProperties equal r.NavigationProperties
   join s in sysdiagram on u.NavigationProperties equals s.NavigationProperties
   select new { u = u, r = r, s = s}
   ).ToList();

相关问题