(Gridview)第一条记录正从数据库中删除c#

时间:2016-08-01 05:15:43

标签: c# asp.net sql-server gridview

<asp:GridView ID="GridView1" runat="server" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None">           
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <Columns>

                 <asp:TemplateField HeaderText="Add Records">
                    <ItemTemplate>
                       <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" OnClick="btnadd_Click" CommandName="insert"
                        Text="Insert"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                 <asp:TemplateField HeaderText="Delete Records">
                    <ItemTemplate>
                       <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="True" OnClick="btndelete_Click" CommandName="delete"
                        Text="delete"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="id">
                    <ItemTemplate>
                        <asp:Label ID="lblid" runat="server" Text='<%#Bind("id") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Client Name">
                    <ItemTemplate>
                        <asp:Label ID="lblname" runat="server" Text='<%#Bind("client_name") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Email">
                    <ItemTemplate>
                        <asp:Label ID="lblemail" runat="server" Text='<%#Bind("email") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Google Email">
                    <ItemTemplate>
                        <asp:Label ID="lblgemail" runat="server" Text='<%#Bind("google_email") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Contact Number">
                    <ItemTemplate>
                        <asp:Label ID="lblcont" runat="server" Text='<%#Bind("contact_number") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="role">
                    <ItemTemplate>
                        <asp:Label ID="lblrole" runat="server" Text='<%#Bind("role") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

            </Columns>
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>

我有一张包含100条记录的表

这是我的Gridview我有两个按钮添加和删除(在删除按钮上点击特定记录应该从数据库中删除)就像,我点击了记录号35(记录号35应该被删除)但是记录号1被删除每次。

public void delete()
{ 

    foreach (GridViewRow g in GridView1.Rows)
    {
        Label lblname = (Label)g.FindControl("lblname");
        Button btnde = (Button)g.FindControl("btndelete");
        //Response.Redirect("cs.aspx");
        SqlCommand cmd = new SqlCommand("delete from clientrequest where client_name='" + lblname.Text + "'", con);
        con.Open();
        cmd.ExecuteNonQuery();
        Response.Redirect("welcome.aspx");
        con.Close();
    }
}



protected void btndelete_Click(object sender, EventArgs e)
{
    delete();
    GridView1.Visible = false;

}

这是我的CS代码。

3 个答案:

答案 0 :(得分:0)

您可以找到您的Label控件位置并获取其值,然后从数据库中删除。它会正常工作。

 public void delete()
    { 

foreach (GridViewRow g in GridView1.Rows)
{
  if(g.RowType == DataControlRowType.DataRow)
{ 
 Label lblname = (Label)g.FindControl("lblname");
 SqlCommand cmd = new SqlCommand("delete from clientrequest where client_name='" + lblname.Text + "'", con);
    con.Open();
    cmd.ExecuteNonQuery();
    Response.Redirect("welcome.aspx");
    con.Close();
    break;
}             
}
}

答案 1 :(得分:0)

这是因为在删除时你使用foreach循环删除记录,这不是一个好方法。你可以试试这个:

public void delete(string Name)
{ 
        SqlCommand cmd = new SqlCommand("delete from clientrequest where client_name='" + Name + "'", con);
        con.Open();
        cmd.ExecuteNonQuery();
        Response.Redirect("welcome.aspx");
        con.Close();
}

在“删除”按钮上单击“查找特定行”并将其发送到“删除方法”

protected void btndelete_Click(object sender, EventArgs e)
{
    Button btn = sender as Button;
    GridViewRow gvr = (GridViewRow)btn.NamingContainer;
    string name = ((Label)gvr.FindControl("lblname")).Text;
    delete(name);
    GridView1.Visible = false;

}

注意:您也可以在删除方法返回的基础上在按钮单击事件中编写重定向。

答案 2 :(得分:-1)

  

在这段代码中你缺少gridview行索引,这就是它在第一行中找到的原因By Default在这种情况下你必须找到Selected row index并像这样使用它

public void delete()
{ 
  foreach (GridViewRow g in GridView1.Rows)
  {
    Label lblname = (Label)g.Rows[rownumber].FindControl("lblname");
    Button btnde = (Button)g.Rows[rownumber].FindControl("btndelete");
    //Response.Redirect("cs.aspx");
    SqlCommand cmd = new SqlCommand("delete from clientrequest where client_name='" + lblname.Text + "'", con);
    con.Open();
    cmd.ExecuteNonQuery();
    Response.Redirect("welcome.aspx");
    con.Close();
}

}

  

并在 GridView1_SelectedIndexChanged 上,您的代码将是这样的

 static int RowNumber;
protected void g_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
   RowNumber= g.SelectedIndex;
}

这可能会对你有帮助。