如何从gridview删除删除?
我尝试了这个,但它不起作用?
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myConnectionString %>" SelectCommand="SELECT * FROM [Groups]" DeleteCommand ="DELETE FROM Groups WHERE GroupID = @GroupID">
<DeleteParameters>
<asp:ControlParameter Name="GroupID" ControlId="DropDownListID" PropertyName="SelectedValue" />
<asp:ControlParameter Name="GroupName" ControlId="DropDownListID" PropertyName="SelectedValue" />
<asp:ControlParameter Name="Description" ControlId="DropDownListID" PropertyName="SelectedValue" />
</DeleteParameters>
Gridview代码:
<asp:GridView ID="GridView_manageGroup" runat="server" AllowPaging="True" AllowSorting="True" CssClass="table table-striped table-bordered table-hover" AutoGenerateColumns="False" DataKeyNames="GroupID" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="GroupID" HeaderText="Group ID" InsertVisible="False" ReadOnly="True" SortExpression="GroupID" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:BoundField DataField="GroupName" HeaderText="Group Name" SortExpression="GroupName" />
<asp:CommandField ShowSelectButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
答案 0 :(得分:3)
由于您已在DataKeyNames="GroupID"
代码
<asp:GridView>
<asp:GridView ID="GridView_manageGroup" runat="server" AllowPaging="True" AllowSorting="True" CssClass="table table-striped table-bordered table-hover" AutoGenerateColumns="False" DataKeyNames="GroupID" DataSourceID="SqlDataSource1">
您需要将<asp:SqlDataSource>
标记更改为此
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myConnectionString %>" SelectCommand="SELECT * FROM [Groups]" DeleteCommand ="DELETE FROM Groups WHERE GroupID = @GroupID">
<DeleteParameters>
<asp:Parameter Name="GroupID" Type="Int32" />
</DeleteParameters>
答案 1 :(得分:0)
试试这个:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myConnectionString %>" SelectCommand="SELECT * FROM [Groups]" DeleteCommand ="DELETE FROM Groups WHERE GroupID = @GroupID">
<DeleteParameters>
<asp:ControlParameter Name="GroupID" ControlId="DropDownListID" PropertyName="SelectedValue" />
</DeleteParameters>
答案 2 :(得分:0)
在aspx中
<asp:TemplateField HeaderText="Delete">
<HeaderStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:ImageButton ID="lnkDelete" runat="server" CssClass="grid-close" CausesValidation="False"
CommandName="Delete" ImageUrl="~/Images/DELETE.png"
CommandArgument='<%# Container.DataItemIndex %>' />
</ItemTemplate>
<ItemStyle Width="75px" HorizontalAlign="Center" />
</asp:TemplateField>
在代码背后
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
Label lblRequestID = (Label)GridView1.Rows[e.RowIndex].FindControl("lblRequestID");
Delete(lblRequestID .Text);
FillGrid();
}
catch (Exception ex)
{
Response.Write(ex.InnerException);
}
}