Hi kankon,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:GridView runat="server" ID="gvEmployees" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox Text="" runat="server" ID="chkRow" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="EmployeeID" HeaderText="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView><br />
<asp:Button Text="Count" runat="server" ID="btnCount" OnClick="OnCount" /><br />
USA : <asp:Label Text="" runat="server" ID="lblUsa" /><br />
UK : <asp:Label Text="" runat="server" ID="lblUk" />
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Linq
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT EmployeeID,FirstName+' '+LastName AS Name,Country FROM Employees", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter da = new SqlDataAdapter())
{
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
this.gvEmployees.DataSource = dt;
this.gvEmployees.DataBind();
this.GetCounts(dt);
}
}
}
}
private void GetCounts(DataTable dt)
{
this.lblUsa.Text = "0";
this.lblUk.Text = "0";
var result = from row in dt.AsEnumerable()
group row by row["Country"] into g
select new
{
country = g.Key,
count = g.Count()
};
foreach (var key in result)
{
if (key.country.ToString().ToLower() == "usa")
{
this.lblUsa.Text = key.count.ToString();
}
else if (key.country.ToString().ToLower() == "uk")
{
this.lblUk.Text = key.count.ToString();
}
}
}
protected void OnCount(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Country");
foreach (GridViewRow row in gvEmployees.Rows)
{
CheckBox cbRow = row.FindControl("chkRow") as CheckBox;
if (cbRow.Checked)
{
string country = row.Cells[3].Text;
dt.Rows.Add(country);
}
}
this.GetCounts(dt);
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Private Sub BindGrid()
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT EmployeeID,FirstName+' '+LastName AS Name,Country FROM Employees", con)
cmd.CommandType = CommandType.Text
Using da As SqlDataAdapter = New SqlDataAdapter()
da.SelectCommand = cmd
Dim dt As DataTable = New DataTable()
da.Fill(dt)
Me.gvEmployees.DataSource = dt
Me.gvEmployees.DataBind()
Me.GetCounts(dt)
End Using
End Using
End Using
End Sub
Private Sub GetCounts(ByVal dt As DataTable)
Me.lblUsa.Text = "0"
Me.lblUk.Text = "0"
Dim result = From row In dt.AsEnumerable()
Group row By key = row("Country") Into Group
Select New With
{
.country = key,
.Count = Group.Count()
}
For Each key In result
If key.country.ToString().ToLower() = "usa" Then
Me.lblUsa.Text = key.Count.ToString()
ElseIf key.country.ToString().ToLower() = "uk" Then
Me.lblUk.Text = key.Count.ToString()
End If
Next
End Sub
Protected Sub OnCount(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = New DataTable()
dt.Columns.Add("Country")
For Each row As GridViewRow In gvEmployees.Rows
Dim cbRow As CheckBox = TryCast(row.FindControl("chkRow"), CheckBox)
If cbRow.Checked Then
Dim country As String = row.Cells(3).Text
dt.Rows.Add(country)
End If
Next
Me.GetCounts(dt)
End Sub
Screenshot