Skip to main content
 首页 » 编程设计

c#之ASP.NET SqlDataSource Gridview 问题

2024年02月13日20zhenyulu

这有几个问题。现在,我要做的就是让删除命令生效。编辑也不起作用,猜测问题是相似的。这是我自己的一个学习项目。任何帮助是极大的赞赏。谢谢。

 <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="Both" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" > 
    <AlternatingRowStyle BackColor="White" /> 
    <EditRowStyle BackColor="#7C6F57" /> 
 
    <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> 
    <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> 
    <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" /> 
    <RowStyle BackColor="#E3EAEB" /> 
    <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" /> 
    <SortedAscendingCellStyle BackColor="#F8FAFA" /> 
    <SortedAscendingHeaderStyle BackColor="#246B61" /> 
    <SortedDescendingCellStyle BackColor="#D4DFE1" /> 
    <SortedDescendingHeaderStyle BackColor="#15524A" /> 
    <Columns> 
 
      <asp:BoundField DataField="MenuID" HeaderText="ID" Visible="true" ReadOnly="true" /> 
 
      <asp:TemplateField HeaderText="Date"> 
        <EditItemTemplate> 
          <asp:TextBox ID="txtGridDate" runat="server" Text='<%# Bind("Date", "{0:M/dd/yyyy}")%>' Width="75px"></asp:TextBox> 
        </EditItemTemplate> 
        <ItemTemplate> 
          <asp:Label ID="lblGridDate" runat="server" Text='<%# Bind("Date", "{0:M/dd/yyyy}") %>'></asp:Label> 
        </ItemTemplate> 
      </asp:TemplateField> 
 
      <asp:TemplateField HeaderText="Description"> 
        <EditItemTemplate> 
          <asp:TextBox ID="txtGridDescription" runat="server" Text='<%# Bind("Description") %>' Width="600px"></asp:TextBox> 
        </EditItemTemplate> 
        <ItemTemplate> 
          <asp:Label ID="lblGridDescription" runat="server" Text='<%# Bind("Description") %>'></asp:Label> 
        </ItemTemplate> 
      </asp:TemplateField> 
 
      <asp:TemplateField HeaderText="MealType"> 
        <EditItemTemplate> 
          <asp:DropDownList ID="ddlGridMealType" runat="server"> 
            <asp:ListItem Value="1" Text="Breakfast"></asp:ListItem> 
            <asp:ListItem Value="2" Text="Dinner"></asp:ListItem> 
          </asp:DropDownList> 
        </EditItemTemplate> 
        <ItemTemplate> 
          <asp:Label ID="lblGridType" runat="server" Text='<%# Bind("MealType") %>'></asp:Label> 
        </ItemTemplate>          
      </asp:TemplateField> 
 
      <asp:CommandField ButtonType="Button" ShowEditButton="true" ValidationGroup="edit" />      
      <asp:CommandField ButtonType="Button" ShowDeleteButton="true" /> 
    </Columns>         
  </asp:GridView> 
  <asp:SqlDataSource  
    ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CampRandolphConnectionString %>"  
    SelectCommand="sproc_sel_AllMeals" SelectCommandType="StoredProcedure"  
    InsertCommand="sproc_ins_Meal" InsertCommandType="StoredProcedure" 
    UpdateCommand="sproc_edit_Meal" UpdateCommandType="StoredProcedure"  
    DeleteCommand="sproc_del_MenuItem" DeleteCommandType="StoredProcedure" > 
    <InsertParameters> 
      <asp:Parameter DbType="Date" Name="Date" /> 
      <asp:Parameter Name="Description" Type="String" /> 
      <asp:Parameter Name="MealCategoryID" Type="Int32" /> 
    </InsertParameters> 
    <UpdateParameters> 
      <asp:Parameter Name="MenuID" Type="Int32" /> 
      <asp:Parameter DbType="Date" Name="Date" /> 
      <asp:Parameter Name="Description" Type="String" /> 
      <asp:Parameter Name="MealCategoryID" Type="Int32" /> 
    </UpdateParameters> 
    <DeleteParameters> 
      <asp:Parameter Name="MenuID" Type="Int32" /> 
    </DeleteParameters> 
  </asp:SqlDataSource> 

这是存储过程:

ALTER PROCEDURE [dbo].[sproc_del_MenuItem] 
@MenuID int 
AS 
BEGIN 
DELETE FROM CampRandolph.dbo.Menu 
WHERE @MenuID = MenuID 
END 

提前致谢。

请您参考如下方法:

您的存储过程中的 Where 子句有误。应该是:

ALTER PROCEDURE [dbo].[sproc_del_MenuItem] 
@MenuID int 
AS 
BEGIN 
DELETE FROM CampRandolph.dbo.Menu 
WHERE MenuID = @MenuID 
END 

在您定义的 GridView 中添加 DataKeyNames 属性:

<asp:GridView ID="GridView1" runat="server" DataKeyNames="MenuID" .... 

我还读到 HeaderText 需要与键名相同:

 <asp:BoundField DataField="MenuID" HeaderText="MenuID" Visible="true" ReadOnly="true" />