How to Delete Rows in Gridview using Checkbox on Asp.Net

Leave a Comment
There are many ways to delete rows from Gridview. but here, we have delete rows using checkbox in gridview.

Delete Rows using checkboxes in Gridview
In this example, I have create a Database called Database.mdf and create one table called Student.
Now, I have add some records of student table.


Now, Create aspx page Defwult.aspx and add following code.

<table>
        <tr>
            <td>
                <asp:Button ID="btn_delete" runat="server" Text="Delete"
                    onclick="btn_delete_Click" />
                <asp:Button ID="btn_select_all" runat="server" Text="Select All"
                    onclick="btn_select_all_Click" />
                <asp:Button ID="btn_select_none" runat="server" Text="Select None"
                    onclick="btn_select_none_Click" />
            </td>
        </tr>
        <tr>
            <td>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CssClass="mGrid"
            PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt">
            <Columns>
                <asp:TemplateField HeaderText="Action">
                    <ItemTemplate>
                        <asp:CheckBox ID="chk_delete" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Id">
                    <ItemTemplate>
                        <asp:Label ID="lbl_studentId" runat="server" Text='<%# Bind("student_id") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:Label ID="lbl_StudentName" runat="server" Text='<%# Bind("student_name") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Roll No.">
                    <ItemTemplate>
                        <asp:Label ID="lbl_studentRoll" runat="server" Text='<%# Bind("student_rollno") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Standard">
                    <ItemTemplate>
                        <asp:Label ID="lbl_studentStd" runat="server" Text='<%# Bind("student_std") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        </td>
        </tr>
    </table>


In above code, we have used three buttons delete, select all, and select none.

Delete button can delete selected checkbox rows. select all button can checked all checkboxes in grideview and select none button unselect all checkboxes from gridview.

Now add following code to the Default.aspx back page. [Default.aspx.cs]

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Student"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadStudentData();
        }
    }
    private void LoadStudentData()
    {
        SqlDataAdapter adp = new SqlDataAdapter("Select * from Student", con);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void btn_delete_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            var chk = row.FindControl("chk_delete") as CheckBox;
            if (chk.Checked)
            {
                var lblid = row.FindControl("lbl_studentId") as Label;
                SqlCommand com = new SqlCommand();
                com.CommandText = "Delete from Student where student_id=@id";
                com.Connection= con;
                com.Parameters.AddWithValue("@id", int.Parse(lblid.Text));
                con.Open();
                com.ExecuteNonQuery();
                con.Close();
                LoadStudentData();
            }
        }
    }
    protected void btn_select_all_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            var chk = row.FindControl("chk_delete") as CheckBox;
            chk.Checked = true;
        }
    }
    protected void btn_select_none_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            var chk = row.FindControl("chk_delete") as CheckBox;
            chk.Checked = false;
        }
    }


Demo

Delete Rows in Gridview using Checkbox

0 comments:

Post a Comment