Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9319899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:30:00+00:00 2026-06-19T03:30:00+00:00

I have created a Gridview containing data extracted from a TextBox. I have a

  • 0

I have created a Gridview containing data extracted from a TextBox. I have a “Delete Link” on the GridView to delete the row from the GridView if required.

Now I want to make some changes to it. Instead of the “Delete link” on the GridView, I want Checkboxes on each row of the GridView. Outside the GridView, there should be a Button. On clicking that button, the rows selected via the Checkbox on the Gridview should be deleted.

What changes has to be made to the below code to implement this functionality? Please specify the changes clearly.

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <br />

        Employee 
        ID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <br />
        Employee Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        <br />
        Salary&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        <br />
        <br />
        <br />
<asp:Button ID="Button1" runat="server" Text="Add to Grid" OnClick="Button1_Click" />
        <br />
        <br />
        <br />
        <br />
        <br />
<asp:Button ID="Button2" runat="server" Text="Export data to Database" OnClick="Button2_Click" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <br />
        <br />
        <asp:Label ID="Label1" runat="server"></asp:Label>
        <br />
        <br />


<asp:GridView ID="GridView1" runat="server" DataKeyNames="EmpID" AutoGenerateColumns="false"
        OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit"
        OnRowDeleting="GridView1_RowDeleting" OnPageIndexChanging="GridView1_PageIndexChanging"
        PageSize="5" AllowPaging="true" OnRowUpdating="GridView1_RowUpdating" Width="800">
    <Columns>
        <asp:TemplateField HeaderText="Employee ID">
            <ItemTemplate>
                <%#Eval("EmpID")%>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Employee Name">
            <ItemTemplate>
                <%#Eval("EmpName")%>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtEmpName" runat="server" Text='<%#Eval("EmpName") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Salary">
            <ItemTemplate>
                <%#Eval("EmpSalary")%>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtEmpSalary" runat="server" Text='<%#Eval("EmpSalary") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:CommandField HeaderText="Modify" ShowEditButton="true" EditText="Edit">
            <ControlStyle Width="50" />
        </asp:CommandField>
        <asp:TemplateField HeaderText="Delete">
            <ItemTemplate>
                <asp:LinkButton ID="lnkDelete" CommandName="Delete" runat="server" OnClientClick="return confirm('Are you sure you want to delete these records?');">Delete</asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

    </div>
    <p style="width: 799px">
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="Button3" runat="server" onclick="Button3_Click" 
            Text="Delete Checked items" Width="162px" />
    </p>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
    <p>
<asp:GridView ID="GridView2" runat="server" BackColor="White" 
            BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" 
            Width="580px">
            <PagerStyle HorizontalAlign="Left" />
</asp:GridView>
    </p>
    </form>
    <p>
        &nbsp;</p>
</body>
</html>

Sample.aspx.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Linq;
using System.Xml.Linq;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection sqlcon = new SqlConnection(ConfigurationManager.AppSettings["constring"]);
    SqlCommand sqlcmd = new SqlCommand();
    SqlDataAdapter da = new SqlDataAdapter();
    DataTable dt = new DataTable();
    DataTable dt1 = new DataTable();
    DataRow dr;
    DataRow dr1;
    DataSet ds = new DataSet();

    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = "";
        //lbldbmsg.Text = "";
        if (!Page.IsPostBack)
        {
            dt.Columns.Add("EmpID");
            dt.Columns.Add("EmpName");
            dt.Columns.Add("EmpSalary");
            Session["reptable"] = dt;
            GridData();            
        }
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        GridData();
    }

    void GridData()
    {
        GridView1.DataSource = (DataTable)Session["reptable"];
        GridView1.DataBind();
    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        GridData();
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.RowIndex];
        string EmpID;

        EmpID = GridView1.DataKeys[e.RowIndex].Value.ToString();

        TextBox EmpName = (TextBox)row.FindControl("txtEmpName");
        TextBox EmpSalary = (TextBox)row.FindControl("txtEmpSalary");


        if (Session["reptable"] != null)
        {
            DataTable dt1 = new DataTable();
            dt1.Clear();
            dt1 = Session["reptable"] as DataTable;
            for (int i = 0; i <= dt1.Rows.Count - 1; i++)
            {
                DataRow dr;
                if (dt1.Rows[i][0].ToString() == EmpID)
                {
                    dr = dt1.Rows[i];
                    dt1.Rows[i].Delete();
                }
            }
            Session.Remove("reptable");
            Session["reptable"] = dt1;

            //add that updated row here
            dt = (DataTable)Session["reptable"];
            dr1 = dt.NewRow();
            dr1["EmpID"] = EmpID;
            dr1["EmpName"] = EmpName.Text;
            dr1["EmpSalary"] = EmpSalary.Text;
            dt.Rows.Add(dr1);
            Session.Remove("reptable");
            Session["reptable"] = dt;
        }

        GridView1.EditIndex = -1;
        GridData();
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string EmpID;
        EmpID = GridView1.DataKeys[e.RowIndex].Value.ToString();
        if (Session["reptable"] != null)
        {
            DataTable dt1 = new DataTable();
            dt1.Clear();
            dt1 = Session["reptable"] as DataTable;
            for (int i = 0; i <= dt1.Rows.Count - 1; i++)
            {
                DataRow dr;
                if (dt1.Rows[i][0].ToString() == EmpID)
                {
                    dr = dt1.Rows[i];
                    dt1.Rows[i].Delete();
                    //dt1.Rows.Remove(dr);
                }
            }
            Session.Remove("reptable");
            Session["reptable"] = dt1;
        }
        GridData();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridData();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        dt = (DataTable)Session["reptable"];
        dr = dt.NewRow();
        dr["EmpID"] = TextBox1.Text;
        dr["EmpName"] = TextBox2.Text;
        dr["EmpSalary"] = TextBox3.Text;
        dt.Rows.Add(dr);
        Session.Remove("reptable");
        Session["reptable"] = dt;
        GridData();
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
    }

    //Bulk Insert data into sql server database
    protected void Button2_Click(object sender, EventArgs e)
    {
        dt = (DataTable)Session["reptable"];
        //Upload data to Database using bulk copy
        SqlBulkCopy sqlBulk = new SqlBulkCopy(ConfigurationManager.AppSettings["constring"]);
        sqlBulk.DestinationTableName = "Emp";          //table name
        sqlBulk.WriteToServer(dt);

        //remove data after insert
        dt.Clear();
        Session["reptable"] = dt;
        GridData();
        Label1.Text = "All Records Inserted successfully into the database";

    }

    protected void Button3_Click(object sender, EventArgs e)
    {

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T03:30:02+00:00Added an answer on June 19, 2026 at 3:30 am

    Try this..

    ASPX Code:

    Add checkbox control in item template of your gridview..

     <asp:TemplateField>
     <ItemTemplate>
     <asp:CheckBox ID="chkdelete" runat="server" />
     </ItemTemplate>
     </asp:TemplateField>
    

    C# code:

       protected void Button3_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gvrow in GridView1.Rows)
        {
            //Finiding checkbox control in gridview for particular row
            CheckBox chkdelete = (CheckBox)gvrow.FindControl("chkdelete");
            //Condition to check checkbox selected or not
            if (chkdelete.Checked)
            {
                if (Session["reptable"] != null)
                {
                    string EmpID = GridView1.DataKeys[gvrow.RowIndex].Value.ToString();
                    DataTable dt1 = new DataTable();
                    dt1.Clear();
                    dt1 = Session["reptable"] as DataTable;
                    for (int i = 0; i <= dt1.Rows.Count - 1; i++)
                    {
                        DataRow dr;
                        if (dt1.Rows[i][0].ToString() == EmpID)
                        {
                            dr = dt1.Rows[i];
                            dt1.Rows[i].Delete();
                            //dt1.Rows.Remove(dr);
                        }
                    }
                    //Session.Remove("reptable");
                    Session["reptable"] = dt1;
                }
               
            }
        }
      GridData();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a gridview for populating data, and some user control (TextBox) to
I have created a gridview with a column of checkboxes. I want the user
in my application im exporting gridview data to excel sheet now i want save
I have created a class doing some jobs like GridView inherit from System.Web.UI.WebControls.WebControl. public
I have created a database and have presented the data in a gridview using
I have created a simple calendar using GridView and whenever I switch from portrait
I have gridview bind in user control and want to access that gridview from
I have created gridview in code behind file of C#. I want to place
I have created GridView with Insert, Edit, and Delete function. The Insert work great
I have created a ASP.NET WebForms Gridview and I need to access each row

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.