无法在 GridView 控件中编辑行

Cannot Edit Rows in GridView Control

我正在尝试将 2 table 的数据从 Microsoft SQL 服务器检索到 GridView 控件。到目前为止,我能够在 GridView 控件中显示我想要的任何内容。但是,我想为用户制作 Editable,我有一个 小问题

这是我的GridView控件源代码如下:

<asp:GridView ID="GridView1" runat="server" CssClass="auto-style36" AllowPaging="True" AutoGenerateColumns="False" 
        BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
        CellSpacing="2" DataSourceID="SqlDataSource1" DataKeyNames="SID">
        <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:BoundField DataField="SID" HeaderText="Student ID" ReadOnly="True" SortExpression="SID" />
            <asp:BoundField DataField="S_FName" HeaderText="First Name" SortExpression="S_FName" />
            <asp:BoundField DataField="S_LName" HeaderText="Last Name" SortExpression="S_LName" />
            <asp:BoundField DataField="Atd_Date" HeaderText="Date" SortExpression="Atd_Date" />
            <asp:BoundField DataField="Atd_InTime" HeaderText="In Time" SortExpression="Atd_InTime" />
            <asp:BoundField DataField="Atd_OutTime" HeaderText="Out Time" SortExpression="Atd_OutTime" />
            <asp:BoundField DataField="Atd_Comment" HeaderText="Comment" SortExpression="Atd_Comment" />
            <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
        </Columns>
        <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
        <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
        <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
        <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#FFF1D4" />
        <SortedAscendingHeaderStyle BackColor="#B95C30" />
        <SortedDescendingCellStyle BackColor="#F1E5CE" />
        <SortedDescendingHeaderStyle BackColor="#93451F" />
    </asp:GridView>

正如您在上面看到的,我正在使用 SqlDataSource1 将数据检索到 GridView:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SAOS1ConnectionString %>" 
        SelectCommand="SELECT attendance.Atd_Date, attendance.Atd_InTime, attendance.Atd_OutTime, attendance.Atd_Comment, 
        attendance.Status, student.SID, student.S_FName, student.S_LName FROM student LEFT OUTER JOIN attendance ON student.SID = attendance.SID 
        AND attendance.Atd_Date = @Atd_Date LEFT OUTER JOIN class ON student.CID = class.CID WHERE (class.CID = @CID)" 
        UpdateCommand="updatetable1" UpdateCommandType="StoredProcedure">
        <SelectParameters>
            <asp:ControlParameter ControlID="datepicker" Name="Atd_Date" PropertyName="Text" />
            <asp:ControlParameter ControlID="LabelClassID" Name="CID" PropertyName="Text" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="Atd_ID" Type="Int32" />
            <asp:Parameter DbType="Date" Name="Atd_Date" />
            <asp:Parameter DbType="Time" Name="Atd_InTime" />
            <asp:Parameter DbType="Time" Name="Atd_OutTime" />
            <asp:Parameter Name="Atd_Comment" Type="String" />
            <asp:Parameter Name="Status" Type="String" />
            <asp:Parameter Name="SID" Type="Int32" />
            <asp:Parameter Name="S_FName" Type="String" />
            <asp:Parameter Name="S_LName" Type="String" />
        </UpdateParameters>
    </asp:SqlDataSource>

对于 UPDATE 语句,我正在使用名为 updatetable1:

的 Stored Procured
    CREATE PROCEDURE [dbo].[updatetable1]
(
    @Atd_ID int,
    @Atd_Date date,
    @Atd_InTime time(7),
    @Atd_OutTime time(7),
    @Atd_Comment varchar(50),
    @Status varchar(50),
    @SID int,
    @S_FName varchar(50),
    @S_LName varchar(50)
)
AS
BEGIN
UPDATE attendance SET Atd_Date=@Atd_Date, Atd_InTime=@Atd_InTime, Atd_OutTime=@Atd_OutTime, Atd_Comment=@Atd_Comment, Status=@Status where Atd_ID=@Atd_ID
UPDATE student SET S_FName=@S_FName, S_LName=@S_LName WHERE SID=@SID
END
RETURN

当我在 GridView 中单击 Edit link 时,来自 table student 的列,例如 SID, S_FName & S_LName 已成功更新。

但是 table attendance 中的其余列无法成功更新。更改保持不变。请帮忙

根据 GridView 定义,有一个名为 DataKeyNames 的 属性,您可以在其中设置关键字段的名称,从而为包含的行启用自动 update/delete在里面。以下是 DataKeyNames property 的简要说明:

You should only set this property to the field or fields that are required to uniquely identify each row; for example, the ID column if an integer value uniquely identifies each row. You must set the DataKeyNames property in order for the automatic update and delete features of the GridView control to work.

根据当前 GridView 状态,您可能需要 attendancestudent table 的主键和外键来启用自动更新,如上所述:

<asp:GridView ID="GridView1" runat="server" CssClass="auto-style36" AllowPaging="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" 
              DataKeyNames="Atd_ID,SID" ...>
</asp:GridView>

另外记得将Atd_ID作为attendancetable的主键添加到SqlDataSource的结果集中:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:SAOS1ConnectionString %>"
            SelectCommand="SELECT attendance.Atd_ID, ..., student.SID, ..." 
            UpdateCommand="updatetable1" UpdateCommandType="StoredProcedure">
</asp:SqlDataSource>