Gridview不更新所有字段

时间:2012-05-21 07:08:08

标签: asp.net gridview

我有一个从3个表中正确填充的gridview。 当我更新它时,它只会更新一个字段(Kommentar),但只是第一次。

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID,Erledigt,Kommentar"
        DataSourceID="ArtikelDataSource">
        <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" 
                ReadOnly="True"  >
            <ControlStyle Width="150px" />
            </asp:BoundField>
            <asp:BoundField DataField="Beschreibung" HeaderText="Beschreibung" 
                SortExpression="Beschreibung" ReadOnly="True" >
            <ControlStyle Width="200px" />
            </asp:BoundField>
            <asp:BoundField DataField="Datum" HeaderText="Datum" SortExpression="Datum" 
                ReadOnly="True" >
            <ControlStyle Width="100px" />
            </asp:BoundField>
            <asp:BoundField DataField="Erledigt" HeaderText="Erledigt" 
                SortExpression="Erledigt" >
            <ControlStyle Width="50px" />
            </asp:BoundField>
            <asp:BoundField DataField="Kommentar" HeaderText="Kommentar" 
                SortExpression="Kommentar" >
            <ControlStyle Width="200px" />
            </asp:BoundField>
            <asp:BoundField DataField="LastUpdate" HeaderText="LastUpdate" 
                SortExpression="LastUpdate" ReadOnly="True" />
            <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
                ReadOnly="True" SortExpression="ID" Visible="False" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="ArtikelDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" 
        SelectCommand="SELECT ci.Name, ci.Beschreibung, ca.ChkItemID, ca.ChkUserID, ca.Datum, ca.Erledigt, ca.Kommentar, ca.LastUpdate, ca.ID FROM ChecklistActions AS ca INNER JOIN ChecklistItems AS ci ON ca.ChkItemID = ci.ID INNER JOIN ChecklistUsers AS cu ON ca.ChkUserID = cu.ID WHERE (ca.Datum = @datum) AND (cu.Login = @userlogin)" 
        UpdateCommand="UPDATE ChecklistActions SET Kommentar = @Kommentar, LastUpdate = GETDATE() WHERE (ID = @ID)"       
        OnSelecting="_data_Selecting"
        >
        <SelectParameters>
            <asp:Parameter Name="userlogin"/>
            <asp:Parameter Name="datum"/>
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="Kommentar" Type="string"/>
            <asp:Parameter Name="ID" />
        </UpdateParameters>
    </asp:SqlDataSource>

我首先是一个空字段“Kommentar”,然后它被填充,但是当我尝试再次更改该值时,只有GETDATE()更改值,Kommentar的内容与之前相同。

当我尝试通过SQL

更改“Erledigt”的值时
UpdateCommand="UPDATE ChecklistActions SET Erledigt = @Erledigt, Kommentar = @Kommentar, LastUpdate = GETDATE() WHERE (ID = @ID)" 
...
<asp:Parameter Name="Erledigt" Type="Int32"/>

除了Getdate()之外什么都没有更新 - 甚至在第一时间都不是Kommentar领域

似乎gridview正在将旧值发送到更新命令(只有第一次更新才会收到新的Kommentar值)这真的很烦人吗?

1 个答案:

答案 0 :(得分:0)

您在UpdateCommand中错过了,

UPDATE ChecklistActions SET Erledigt = @Erledigt, Kommentar = @Kommentar, LastUpdate = GETDATE() WHERE (ID = @ID)"

修改

仅将数据库表上的主键列名称设置为DataKeyNames,在这种情况下,您只更新ChecklistActions表。所以将ChecklistActions的主键设置为DataKeyNames