Delete in Gridview With DropdownListBox

Posted by Unknown On Tuesday, 19 March 2013 0 comments
First Design The page take one Gridview.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True"
                        oncheckedchanged="CheckBox2_CheckedChanged" />
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Id">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%#Eval("id") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Name">
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%#Eval("name") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
 
Design as follow
 
  
 
 
Now write code in .aspx.cs file
 
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList dl = (DropDownList)sender;
        GridViewRow rw = (GridViewRow)dl.Parent.Parent;
       
        DropDownList dl1 = (DropDownList)rw.FindControl("DropDownList1");
        if (dl1.SelectedItem.Text == "Delete")
        {
            Label id = (Label)rw.FindControl("Label1");
            con.Open();
            SqlCommand cmd = new SqlCommand("delete from cust where id='" + id.Text + "'", con);
            cmd.ExecuteNonQuery();
            con.Close();
            grid();
            //Response.Write(id.Text);
        }

    }
Put autopostback event true of dropdown.
 

0 comments:

Post a Comment