For Check All Checkboxes you need to refer Implement check all checkbox functionality in ASP.Net GridView control using JavaScript
Html Code
<div>
<asp:GridView ID="gvEmployees" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkEmployee" runat="server" AutoPostBack="true" OnCheckedChanged="SaveEmployees" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="txtEmpId" runat="server" Text='<%#Eval("empid")%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="txtEmpName" runat="server" Text='<%#Eval("empname")%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="txtCompName" runat="server" Text='<%#Eval("compname")%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gvEmployees.DataSource = this.GetData();
gvEmployees.DataBind();
}
}
private DataTable GetData()
{
string connection = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection con = new SqlConnection(connection))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Employees_1", con))
{
using (SqlDataAdapter da = new SqlDataAdapter())
{
da.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
da.Fill(dt);
return dt;
}
}
}
}
}
protected void SaveEmployees(object sender, EventArgs e)
{
foreach (GridViewRow row in gvEmployees.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkEmployee = (CheckBox)row.FindControl("chkEmployee");
if (chkEmployee.Checked)
{
TextBox txtEmpId = (TextBox)row.FindControl("txtEmpId");
TextBox txtEmpName = (TextBox)row.FindControl("txtEmpName");
TextBox txtCompName = (TextBox)row.FindControl("txtCompName");
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conSTring"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Employees_2 VALUES(@empid,@empname,@compname)", con))
{
cmd.Parameters.AddWithValue("@empid", txtEmpId.Text.Trim());
cmd.Parameters.AddWithValue("@empname", txtEmpName.Text.Trim());
cmd.Parameters.AddWithValue("@compname", txtCompName.Text.Trim());
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
}
}