Hi narasiman,
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Selectdata">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Slipno" HeaderText="Slipno" />
<asp:BoundField DataField="Slipdate" HeaderText="Date" />
<asp:BoundField DataField="Location" HeaderText="Location" />
<asp:BoundField DataField="Section" HeaderText="Section" />
<asp:BoundField DataField="Status" HeaderText="Status" />
</Columns>
</asp:GridView>
<br />
<asp:Button Text="Save" runat="server" OnClick="Save" />
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.AddRange(new System.Data.DataColumn[] {
new System.Data.DataColumn("Slipno", typeof(int)),
new System.Data.DataColumn("Slipdate", typeof(string)),
new System.Data.DataColumn("Location",typeof(int)),
new System.Data.DataColumn("Section",typeof(string)),
new System.Data.DataColumn("Status",typeof(string))});
dt.Rows.Add(123, "28.05.2018", 1001, "Sordi", "New");
dt.Rows.Add(123, "28.05.2018", 1002, "Ferke", "New");
dt.Rows.Add(124, "28.05.2018", 1003, "Sordi", "New");
dt.Rows.Add(124, "28.05.2018", 1004, "Sordi", "New");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void Save(object sender, EventArgs e)
{
List<int> slipNos = new List<int>();
foreach (GridViewRow row in GridView1.Rows)
{
if ((row.FindControl("chkSelect") as CheckBox).Checked)
{
slipNos.Add(Convert.ToInt32(row.Cells[1].Text.Trim()));
}
}
List<int> duplicateSlips = slipNos.GroupBy(x => x).Where(g => g.Count() > 1).Select(y => y.Key).ToList();
if (duplicateSlips.Count > 0)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('For selected slipno " + string.Join(",", duplicateSlips) + " checkbox is selected.')", true);
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As System.Data.DataTable = New System.Data.DataTable()
dt.Columns.AddRange(New System.Data.DataColumn() {
New System.Data.DataColumn("Slipno", GetType(Integer)),
New System.Data.DataColumn("Slipdate", GetType(String)),
New System.Data.DataColumn("Location", GetType(Integer)),
New System.Data.DataColumn("Section", GetType(String)),
New System.Data.DataColumn("Status", GetType(String))})
dt.Rows.Add(123, "28.05.2018", 1001, "Sordi", "New")
dt.Rows.Add(123, "28.05.2018", 1002, "Ferke", "New")
dt.Rows.Add(124, "28.05.2018", 1003, "Sordi", "New")
dt.Rows.Add(124, "28.05.2018", 1004, "Sordi", "New")
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
Dim slipNos As List(Of Integer) = New List(Of Integer)()
For Each row As GridViewRow In GridView1.Rows
If (TryCast(row.FindControl("chkSelect"), CheckBox)).Checked Then
slipNos.Add(Convert.ToInt32(row.Cells(1).Text.Trim()))
End If
Next
Dim duplicateSlips As List(Of Integer) = slipNos.GroupBy(Function(x) x).Where(Function(g) g.Count() > 1).[Select](Function(y) y.Key).ToList()
If duplicateSlips.Count > 0 Then
ClientScript.RegisterClientScriptBlock(Me.[GetType](), "", "alert('For selected slipno " & String.Join(",", duplicateSlips) & " checkbox is selected.')", True)
End If
End Sub
Screenshot