【问题标题】:delete in c#.net with gridview [duplicate]使用gridview在c#.net中删除[重复]
【发布时间】:2016-01-30 06:39:41
【问题描述】:

我正在尝试学习 C#.net,现在我想用 gridview 删除 这是我的代码

<asp:GridView OnRowDeleted="catGrid_RowDeleted" ID="catGrid" runat="server" AutoGenerateColumns="False" DataSourceID="gridSqlCat" CssClass="gridStyle">
                    <Columns>
                        <asp:BoundField DataField="dor_category" HeaderText="دسته بندی ها" SortExpression="dor_category" />
                        <asp:ButtonField  Text="حذف" />
                    </Columns>
                </asp:GridView>

还有这个cs代码

protected void catGrid_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {

    }

那我该怎么办?

【问题讨论】:

  • 你要删除什么?
  • 你的cs代码没有任何代码......

标签: c# asp.net gridview


【解决方案1】:

这个例子很简单:

<asp:GridView ID="GridView1"runat="server" AutoGenerateColumns="false">
<Columns>
    <asp:BoundField DataField="CustomerId" HeaderText="Id" HeaderStyle-Width="30"/>
    <asp:BoundField DataField="Name" HeaderText="Name" HeaderStyle-Width="150"/>
    <asp:BoundField DataField="Country" HeaderText="Country" HeaderStyle-Width="150"/>
    <asp:TemplateField HeaderStyle-Width="50">
        <ItemTemplate>
            <asp:HiddenField ID="hfCustomerId" runat="server" Value='<%# Eval("CustomerId") %>'/>
            <asp:LinkButton ID="lnkDelete" Text="Delete" runat="server"/>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>
</asp:GridView>

还有cs:

[WebMethod]
public static bool DeleteCustomer(int customerId)
{
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlCommand cmd = new SqlCommand("DELETE FROM Customers WHERE CustomerId = @CustomerId"))
        {
            cmd.Connection = con;
            cmd.Parameters.AddWithValue("@CustomerId", customerId);
            con.Open();
            int rowsAffected = cmd.ExecuteNonQuery();
            con.Close();
            return rowsAffected > 0;
        }
    }
}

【讨论】:

    猜你喜欢
    • 2019-03-13
    • 1970-01-01
    • 1970-01-01
    • 2012-07-03
    • 2012-03-25
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 2023-04-09
    相关资源
    最近更新 更多