Hi lingers,
Please refer below code.
HTML
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" OnCheckedChanged="OnChckedChanged" Text="Add" AutoPostBack="True" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="CylinderNo" HeaderText="Cylinder No." />
</Columns>
</asp:GridView>
<asp:Button Text="Save" runat="server" OnClick="OnSave" />
</div>
Namespaces
C#
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Collections
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Id"), new DataColumn("CylinderNo") });
dt.Rows.Add(3083, "r340");
dt.Rows.Add(3084, "r341");
dt.Rows.Add(3085, "r342");
dt.Rows.Add(3086, "r343");
dt.Rows.Add(3087, "r344");
dt.Rows.Add(3088, "r345");
dt.Rows.Add(3089, "r346");
dt.Rows.Add(3080, "r347");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void OnChckedChanged(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
GridViewRow row = (GridViewRow)chk.NamingContainer;
string id = row.Cells[1].Text;
string number = row.Cells[2].Text;
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
if (chk.Checked)
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Tracking VALUES(@Id, @CylinderNo)", con))
{
cmd.Parameters.AddWithValue("@Id", id);
cmd.Parameters.AddWithValue("@CylinderNo", number);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
ScriptManager.RegisterStartupScript(this.GetType(), "alertMessage", "alert('Record Inserted successfully.')", true);
}
else
{
using (SqlCommand cmd = new SqlCommand("DELETE FROM Tracking WHERE cylinderid = @Id", con))
{
cmd.Parameters.AddWithValue("@Id", id);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
ScriptManager.RegisterStartupScript(this.GetType(), "alertMessage", "alert('Record Deleted successfully')", true);
}
}
}
protected void OnSave(object sender, EventArgs e)
{
ArrayList array = new ArrayList();
foreach (GridViewRow row in GridView1.Rows)
{
if ((row.FindControl("chkSelect") as CheckBox).Checked)
{
array.Add(row.Cells[1].Text);
}
}
ScriptManager.RegisterStartupScript(this.GetType(), "alertMessage", "alert('Checked IDs : " + string.Join(",", array.ToArray()) + "')", true); ;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(1) {New DataColumn("Id"), New DataColumn("CylinderNo")})
dt.Rows.Add(3083, "r340")
dt.Rows.Add(3084, "r341")
dt.Rows.Add(3085, "r342")
dt.Rows.Add(3086, "r343")
dt.Rows.Add(3087, "r344")
dt.Rows.Add(3088, "r345")
dt.Rows.Add(3089, "r346")
dt.Rows.Add(3080, "r347")
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Protected Sub OnChckedChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim chk As CheckBox = CType(sender, CheckBox)
Dim row As GridViewRow = CType(chk.NamingContainer, GridViewRow)
Dim id As String = row.Cells(1).Text
Dim number As String = row.Cells(2).Text
Dim constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constring)
If chk.Checked Then
Using cmd As SqlCommand = New SqlCommand("INSERT INTO Tracking VALUES(@Id, @CylinderNo)", con)
cmd.Parameters.AddWithValue("@Id", id)
cmd.Parameters.AddWithValue("@CylinderNo", number)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
ScriptManager.RegisterStartupScript(Me.GetType(), "alertMessage", "alert('Record Inserted successfully.')", True)
Else
Using cmd As SqlCommand = New SqlCommand("DELETE FROM Tracking WHERE cylinderid = @Id", con)
cmd.Parameters.AddWithValue("@Id", id)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
ScriptManager.RegisterStartupScript(Me.GetType(), "alertMessage", "alert('Record Deleted successfully')", True)
End If
End Using
End Sub
Protected Sub OnSave(ByVal sender As Object, ByVal e As EventArgs)
Dim array As ArrayList = New ArrayList()
For Each row As GridViewRow In GridView1.Rows
If (TryCast(row.FindControl("chkSelect"), CheckBox)).Checked Then
array.Add(row.Cells(1).Text)
End If
Next
ScriptManager.RegisterStartupScript(Me.GetType(), "alertMessage", "alert('Checked IDs : " & String.Join(",", array.ToArray()) & "')", True)
End Sub
Screenshot