网格视图从SQL DB中的表中删除记录

时间:2014-02-04 11:06:49

标签: c# sql gridview

我使用网格视图创建了一个Asp.net Web应用程序。我通过从SQl数据库中获取数据来填充网格视图。 如何通过在网格视图中选择它来从表中删除记录。 我尝试了不同的方法,但没有用。

protected void LeaveGrid_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow row = LeaveGrid.SelectedRow; 
    DBConn dbcon = new DBConn();
    String qry = "Delete from Leave where  "What to give here ?"
    Boolean stat = dbcon.inserterrortrack(qry);
    if (stat == true)
    {
        Label1.Text = "Record Deleted Successfully";
    }
    else if (stat == false)
    {
        Label1.Text = "Delete Failed";
    }
    bind();
}

4 个答案:

答案 0 :(得分:1)

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    string id = GridView1.DataKeys[e.RowIndex].Values["Id"].ToString();
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "Delete FROM TableName where Id='" + id + "'";
    cmd.Connection = con;
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
    GetGridData();
    Response.Write("<script>alert('Module Deleted..!!!');</script>");
}

和aspx页面

<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
            GridLines="None" AutoGenerateColumns="False"
            DataKeyNames="Id" AllowPaging="True"                 
            onrowdeleting="GridView1_RowDeleting">   
            <Columns>
             <asp:TemplateField HeaderText="Id">
                    <ItemTemplate>
                        <asp:Label ID="lblID" runat="server" Text='<%# Bind("Id") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>                               
                // here your other colums                   
                <asp:TemplateField HeaderText="Delete?">
                    <ItemTemplate>
                        <span onclick="return confirm('Are you sure to Delete the record?')">
                            <asp:LinkButton ID="lnkB" runat="Server" Text="Delete" CommandName="Delete"></asp:LinkButton>
                        </span>
                    </ItemTemplate>
                </asp:TemplateField>
    </Columns>          
            <AlternatingRowStyle BackColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <sortedascendingcellstyle backcolor="#F5F7FB" />
            <sortedascendingheaderstyle backcolor="#6D95E1" />
            <sorteddescendingcellstyle backcolor="#E9EBEF" />
            <sorteddescendingheaderstyle backcolor="#4870BE" />
        </asp:GridView>

供参考使用该链接:

Edit,update, delete gridview

答案 1 :(得分:0)

您的示例代码没有有效的T-SQL。试试这个:

GridViewRow row = LeaveGrid.SelectedRow; 
DBConn dbcon = new DBConn();
int idToDelete = Convert.ToInt32(LeaveGrid.DataKeys[e.RowIndex].Value.ToString());
String qry = "Delete from Leave where id=" + idToDelete;

答案 2 :(得分:0)

您可以在网格中为每行添加删除命令链接

一个快速的谷歌搜索给了我这个教程
http://www.c-sharpcorner.com/UploadFile/9f0ae2/gridview-edit-delete-and-update-in-Asp-Net/

答案 3 :(得分:0)

如果您使用的是模板字段,

 protected void GridView1_RowDeleting(object sender, GridViewEditEventArgs e)
{  // you can retrieve the value of each row like
  Label lblRequestID=(Label)GridView1.Rows[e.NewEditIndex].FindControl("ControlID");
   Delete(lblRequestID.Text);
}

//如果你使用存储过程

,你可以使用下面的代码
 public void Delete(string id)
    {

        CMD.Parameters.Clear();
        CMD.CommandText = "SP name";
        CMD.Parameters.AddWithValue("@ID", id);
        try
        {
            CON.Open();
            CMD.ExecuteReader();
        }
        catch (Exception ex)
        {
            Console.Write(ex.InnerException);
        }

        finally
        {
            CON.Close();
        }
    }