none
GridView 复合主键更新及删除问题 RRS feed

  • 问题

  • 问题:

    DataKeyNames不设置时能作删除操作,但是不能更新.

    DataKeyNames设置成MID,或者NO,MID时,能删除,不能更新.

    字段NO,MID为复合主键.

    问题的关键在SelectCommand,如果只是单表查询 Select * From inventoryD,DatakeyNames的值会自动生成NO,MID,这样更新删除都没有问题的.

    SelectCommand换成下面代码中的就不行了.郁闷啊!

    源代码如下:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
    
    
    
    
    
    
    
    <%@ Register Assembly="Infragistics2.WebUI.UltraWebGrid.v7.3, Version=7.3.20073.38, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb"
    
    
    
     Namespace="Infragistics.WebUI.UltraWebGrid" TagPrefix="igtbl" %>
    
    
    
    <%@ Register Assembly="WebControl" Namespace="Voodooer.Soft.WebControl" TagPrefix="cc1" %>
    
    
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    
    
    
    
    
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    
    
    
    <head runat="server">
    
    
    
     <title>无标题页</title>
    
    
    
    </head>
    
    
    
    <body>
    
    
    
     <form id="form1" runat="server">
    
    
    
     <div>
    
    
    
     <asp:TextBox ID="tquery" runat="server" Font-Size="Small" Width="132px"></asp:TextBox><asp:AccessDataSource
    
    
    
      ID="ads_inventoryD" runat="server" DataFile="~/App_Data/db.mdb" DeleteCommand="DELETE FROM [inventoryD] WHERE [no] = ? AND [mid] = ?"
    
    
    
      InsertCommand="INSERT INTO [inventoryD] ([no], [mid], [whid], [oAmount], [Amount]) VALUES (?, ?, ?, ?, ?)" SelectCommand="SELECT [NO , Whid, mid, (SELECT [NO] From material where [ID]=D.mid)as [Name], (SELECT [model] From material where [ID]=D.mid)as [Model], oAmount,Amount,Amount-oAmount as dAmount, (SELECT [Unit] From material where [ID]=D.mid)as [Unit]&#13;&#10;FROM inventoryD D WHERE [NO]=?;"
    
    
    
      UpdateCommand="UPDATE [inventoryD] SET [whid] = ?, [oAmount] = ?, [Amount] = ? WHERE [no] = ? AND [mid] = ?">
    
    
    
      <DeleteParameters>
    
    
    
      <asp:Parameter Name="no" Type="Int32" />
    
    
    
      <asp:Parameter Name="mid" Type="Int32" />
    
    
    
      </DeleteParameters>
    
    
    
      <UpdateParameters>
    
    
    
      <asp:Parameter Name="whid" Type="Int32" />
    
    
    
      <asp:Parameter Name="oAmount" Type="Double" />
    
    
    
      <asp:Parameter Name="Amount" Type="Double" />
    
    
    
      <asp:Parameter Name="no" Type="Int32" />
    
    
    
      <asp:Parameter Name="mid" Type="Int32" />
    
    
    
      </UpdateParameters>
    
    
    
      <SelectParameters>
    
    
    
      <asp:ControlParameter ControlID="tquery" Name="?" PropertyName="Text" />
    
    
    
      </SelectParameters>
    
    
    
      <InsertParameters>
    
    
    
      <asp:Parameter Name="no" Type="Int32" />
    
    
    
      <asp:Parameter Name="mid" Type="Int32" />
    
    
    
      <asp:Parameter Name="whid" Type="Int32" />
    
    
    
      <asp:Parameter Name="oAmount" Type="Double" />
    
    
    
      <asp:Parameter Name="Amount" Type="Double" />
    
    
    
      </InsertParameters>
    
    
    
     </asp:AccessDataSource>
    
    
    
     &nbsp;</div>
    
    
    
     <cc1:_gridview id="_GridView1" runat="server" autogeneratecolumns="False" currentpage="0"
    
    
    
      datakeynames="NO,mid" datamember="DefaultView" datasourceid="ads_inventoryD" emptydatatext="NO DATA!"
    
    
    
      font-size="Small" forecolor="#333333" onrowcreated="_GridView1_RowCreated"
    
    
    
      pagingstyle="None" width="720px" OnRowUpdated="_GridView1_RowUpdated" OnRowUpdating="_GridView1_RowUpdating"><Columns>
    
    
    
      <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
    
    
    
      <asp:BoundField DataField="NO" HeaderText="NO" ReadOnly="True" SortExpression="NO" />
    
    
    
      <asp:BoundField DataField="Whid" HeaderText="Whid" SortExpression="Whid" />
    
    
    
      <asp:BoundField DataField="mid" HeaderText="mid" ReadOnly="True" SortExpression="mid" />
    
    
    
      <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
    
    
    
      <asp:BoundField DataField="Model" HeaderText="Model" SortExpression="Model" />
    
    
    
      <asp:BoundField DataField="oAmount" HeaderText="oAmount" SortExpression="oAmount" />
    
    
    
      <asp:BoundField DataField="Amount" HeaderText="Amount" SortExpression="Amount" />
    
    
    
      <asp:BoundField DataField="dAmount" HeaderText="dAmount" ReadOnly="True" SortExpression="dAmount" />
    
    
    
      <asp:BoundField DataField="Unit" HeaderText="Unit" SortExpression="Unit" />
    
    
    
    </Columns>
    
    
    
    
    
    
    
    <RowStyle BackColor="#F1F4F8" Wrap="False"></RowStyle>
    <SelectedRowStyle BackColor="#CFE4F9"></SelectedRowStyle>
    <HeaderStyle BackColor="#A4CEF9" ForeColor="Black" Wrap="False" Font-Size="Small"></HeaderStyle>
    <AlternatingRowStyle BackColor="White"></AlternatingRowStyle>
    </cc1:_gridview>
     </form>
    </body>
    </html>
    
    

    QQ:58413403 Email:voodooer@126.com
    2010年5月5日 8:28

答案

全部回复