如何将DataGridView与两个表绑定,并在WPF中执行CRUD操作

本文关键字:并在 绑定 WPF 操作 CRUD 执行 两个 DataGridView | 更新日期: 2023-09-27 18:17:17

下面是我的表格结构:

第一个表:

CREATE TABLE [dbo].[tblToolModification_Pramary]
(
    [id] [int] IDENTITY(1,1) NOT NULL,
    [ReportNo] [varchar](50) NOT NULL,
    [ToolModificationDate] [varchar](50) NULL,
    [ProductCode] [varchar](50) NULL,
    [ProductionDescription] [varchar](50) NULL,
    [DrawingNo] [varchar](50) NULL,
    [DIENo] [varchar](50) NULL,
    [NoOfCavity] [varchar](50) NULL,
    [DIEMakerCode] [varchar](50) NULL,
    [DIEMakerName] [varchar](50) NULL,
    CONSTRAINT [PK_tblToolModification_Pramary] 
        PRIMARY KEY CLUSTERED ([ReportNo] ASC)
                    WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, 
                          IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[tblToolModification_Foreign]
(
    [id] [int] IDENTITY(1,1) NOT NULL,
    [ReportNo] [varchar](50) NOT NULL,
    [SrlNo] [varchar](50) NULL,
    [DrawingDimension] [varchar](50) NULL,
    [RequiredDimension] [varchar](50) NULL,
    [ActualInDie] [varchar](50) NULL,
    [ActionInNeeded] [varchar](50) NULL,
    [ResultModification] [varchar](50) NULL,
    CONSTRAINT [PK_tblToolModification_Foriegn] 
        PRIMARY KEY CLUSTERED ([id] ASC)
                    WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, 
                          IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

这是我的设计:

<Window x:Class="demo.Foundry.Sample.Tool_Modification_View"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:shell="http://schemas.microsoft.com/winfx/2006/xaml/presentation/shell"
        xmlns:col="clr-namespace:System.Collections;assembly=mscorlib"
        Icon="/img'F_Logo.png"
        WindowStyle="None"
        ShowInTaskbar="False"
        Title="Tool Modification View"
        ResizeMode="NoResize"
        Height="692"
        Width="1591.9"
        Background="#d9f9f7"
        Loaded="Window_Loaded">
    <Grid Margin="0,0,0,-45">
        <Label Content="Tool Modification View"
               VerticalAlignment="Top"
               Height="25"
               HorizontalContentAlignment="Center"
               Background="{DynamicResource {x:Static SystemColors.ActiveCaptionBrushKey}}" />
        <Grid Margin="10,30,4,29">
            <Rectangle HorizontalAlignment="Left"
                       Fill="DarkGreen"
                       Height="50"
                       Margin="61,0,0,0"
                       Stroke="Black"
                       VerticalAlignment="Top"
                       Width="1163" />
            <Label Content="Tool Modification View"
                   Foreground="White"
                   FontWeight="Bold"
                   HorizontalAlignment="Left"
                   Margin="575,11,0,0"
                   VerticalAlignment="Top"
                   Width="155"
                   RenderTransformOrigin="0.03,0.423"
                   Height="27" />
            <DataGrid Name="dgDIE_Outward_DC"
                      HorizontalAlignment="Left"
                      Margin="61,50,0,0"
                      VerticalAlignment="Top"
                      ItemsSource="{Binding}"
                      AutoGenerateColumns="False"
                      Width="1163"
                      Height="310"
                      AlternatingRowBackground="{DynamicResource {x:Static SystemColors.ActiveCaptionBrushKey}}">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding ReportNo, NotifyOnSourceUpdated=True, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                        Width="80"
                                        Header="Report No"
                                        IsReadOnly="True" />
                    <DataGridTextColumn Binding="{Binding ToolModificationDate, NotifyOnSourceUpdated=True, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                        Width="80"
                                        Header="Date" />
                    <DataGridTextColumn Binding="{Binding SrlNo, NotifyOnSourceUpdated=True, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                        Width="50"
                                        Header="Sr.No" />
                    <DataGridTextColumn Binding="{Binding ProductCode, NotifyOnSourceUpdated=True, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                        Width="80"
                                        Header="Product" />
                    <DataGridTextColumn Binding="{Binding ProductionDescription, NotifyOnSourceUpdated=True, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                        Width="150"
                                        Header="Description" />
                    <DataGridTextColumn Binding="{Binding DrawingNo, NotifyOnSourceUpdated=True, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                        Width="100"
                                        Header="Drawing" />
                    <DataGridTextColumn Binding="{Binding DIENo, NotifyOnSourceUpdated=True, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                        Width="55"
                                        Header="DIE No." />
                    <DataGridTextColumn Binding="{Binding NoOfCavity, NotifyOnSourceUpdated=True, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                        Width="55"
                                        Header="Cavity" />
                    <DataGridTextColumn Binding="{Binding DIEMakerName, NotifyOnSourceUpdated=True, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                        Width="100"
                                        Header="Vendor Name" />
                    <DataGridTextColumn Binding="{Binding DrawingDimension, NotifyOnSourceUpdated=True, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                        Width="150"
                                        Header="Drawing Dimension" />
                    <DataGridTextColumn Binding="{Binding RequiredDimension, NotifyOnSourceUpdated=True, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                        Width="150"
                                        Header="Required Dimension" />
                    <DataGridTextColumn Binding="{Binding ActualInDie, NotifyOnSourceUpdated=True, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                        Width="150"
                                        Header="Actual In DIE" />
                    <DataGridTextColumn Binding="{Binding ActionInNeeded, NotifyOnSourceUpdated=True, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                        Width="150"
                                        Header="Action Needed" />
                    <DataGridTextColumn Binding="{Binding ResultModification, NotifyOnSourceUpdated=True, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                        Width="200"
                                        Header="Result After Modification" />
                </DataGrid.Columns>
            </DataGrid>
            <Rectangle HorizontalAlignment="Left"
                       Fill="Beige"
                       Height="50"
                       Margin="62,356,0,0"
                       Stroke="Black"
                       VerticalAlignment="Top"
                       Width="1163" />
            <Label  Content="Report No."
                    HorizontalAlignment="Left"
                    Margin="62,365,0,0"
                    VerticalAlignment="Top"
                    RenderTransformOrigin="1.639,0.468"
                    Width="65" />
            <ComboBox TabIndex="6"
                      HorizontalAlignment="Left"
                      Margin="127,365,0,0"
                      VerticalAlignment="Top"
                      Width="150"
                      Height="28"
                      RenderTransformOrigin="0.474,1.094" />
            <Label  Content="Product"
                    HorizontalAlignment="Left"
                    Margin="277,365,0,0"
                    VerticalAlignment="Top"
                    RenderTransformOrigin="1.639,0.468"
                    Width="65" />
            <ComboBox TabIndex="6"
                      HorizontalAlignment="Left"
                      Margin="331,365,0,0"
                      VerticalAlignment="Top"
                      Width="150"
                      Height="28"
                      RenderTransformOrigin="0.474,1.094" />
            <Label  Content="Drawing"
                    HorizontalAlignment="Left"
                    Margin="495,365,0,0"
                    VerticalAlignment="Top"
                    RenderTransformOrigin="1.639,0.468"
                    Width="65" />
            <ComboBox TabIndex="6"
                      HorizontalAlignment="Left"
                      Margin="550,366,0,0"
                      VerticalAlignment="Top"
                      Width="150"
                      Height="28"
                      RenderTransformOrigin="0.474,1.094" />
            <Button Name="btnExit"
                    Content="Exit"
                    HorizontalAlignment="Left"
                    Margin="843,360,0,0"
                    VerticalAlignment="Top"
                    Width="231"
                    Click="btnExit_Click" />
        </Grid>
    </Grid>
</Window>

我想在这个DataGridView中进行CRUD操作-我如何实现?请帮助. .

所有的操作都应该在这个datagridview删除操作中执行,当我们点击键盘删除按钮时执行。在datagridview列内执行编辑和插入操作

如何将DataGridView与两个表绑定,并在WPF中执行CRUD操作

你不能一次绑定2个表。您需要重新考虑您的数据库设计,或者您需要将两个表作为单个查询,可能使用联合。