Here I have created sample that shows the number of selected row count.
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkHobby" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Hobby" HeaderText="Hobby" ItemStyle-Width="150px" ReadOnly="true" />
</Columns>
</asp:GridView>
<asp:Button Text="Get Selected Count" runat="server" OnClick="GetSelectedCount" /><br />
<br />
<asp:Label ID="lblCount" runat="server" />
</div>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT [HobbyId], [Hobby], [IsSelected] FROM Hobbies"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
protected void GetSelectedCount(object sender, EventArgs e)
{
int count = 0;
foreach (GridViewRow row in GridView1.Rows)
{
if (((CheckBox)row.FindControl("chkHobby")).Checked)
{
count++;
}
}
lblCount.Text = "Selected count : " + count;
}
Screenshot
