title: Count total rows of gridview with pagination
body: <p>I have a GridView with paging. When I try to count gridview row with gridview.rows.count, It gives me row count for current page only. </p>
  
  <p>How can I get total rows of GridView irrespective of page index?</p>
  
  <p>// Here is the code of .aspx page</p>
  
  <pre><code>&lt;%@ Page Title="Search candidates based on vacancy" Language="C#" MasterPageFile="~/HR     Department/hrmasterpage.master"
     AutoEventWireup="true" CodeFile="searcAppForVac.aspx.cs"     Inherits="HR_Department_searcAppForVac" %&gt;
  
  &lt;asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"&gt;&lt;/asp:Content&gt;
  &lt;asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"       runat="Server"&gt;    &lt;table width="100%"&gt;
      &lt;tr&gt;
          &lt;td&gt;
              &amp;nbsp;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td align="center" class="tdtitle"&gt;
              Search Candidates
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;
              &lt;asp:ScriptManager ID="ScriptManager1" runat="server"&gt;
              &lt;/asp:ScriptManager&gt;
              &lt;asp:UpdatePanel ID="UpdatePanel1" runat="server"&gt;
                  &lt;ContentTemplate&gt;
                      &lt;table width="100%"&gt;
                          &lt;tr&gt;
                              &lt;td class="RowHeight" width="20%"&gt;
                                  Select Company
                              &lt;/td&gt;
                              &lt;td width="30%"&gt;
                                  &lt;asp:DropDownList ID="companyList" runat="server" AppendDataBoundItems="true" AutoPostBack="True"
                                      OnSelectedIndexChanged="companyList_SelectedIndexChanged" Width="150px"&gt;
                                      &lt;asp:ListItem Text="-Select Company-" Value="-1"&gt;&lt;/asp:ListItem&gt;
                                  &lt;/asp:DropDownList&gt;
                              &lt;/td&gt;
                              &lt;td width="20%"&gt;
                                  Select Department
                              &lt;/td&gt;
                              &lt;td width="30%"&gt;
                                  &lt;asp:DropDownList ID="deptList" runat="server" AppendDataBoundItems="true" AutoPostBack="True"
                                      onclick="Validate();" OnSelectedIndexChanged="deptList_SelectedIndexChanged"
                                      Width="150px"&gt;
                                      &lt;asp:ListItem Value="-1"&gt;-Select Department-&lt;/asp:ListItem&gt;
                                  &lt;/asp:DropDownList&gt;
                              &lt;/td&gt;
                          &lt;/tr&gt;
                          &lt;tr&gt;
                              &lt;td class="RowHeight" width="20%"&gt;
                                  Select Vacancy
                              &lt;/td&gt;
                              &lt;td colspan="3" width="*"&gt;
                                  &lt;asp:DropDownList ID="vacanyList" runat="server" AppendDataBoundItems="true" 
                                      Width="200px" AutoPostBack="True" 
                                      onselectedindexchanged="vacanyList_SelectedIndexChanged"&gt;
                                      &lt;asp:ListItem Value="-1"&gt;-Select Vacancy-&lt;/asp:ListItem&gt;
                                  &lt;/asp:DropDownList&gt;
                              &lt;/td&gt;
                          &lt;/tr&gt;
                          &lt;tr&gt;
                              &lt;td colspan="4" align="center"&gt;
                                  &amp;nbsp;
                                  &lt;asp:Label ID="notifyLbl" runat="server" Font-Size="Large" ForeColor="Red" 
                                      Text="Label"&gt;&lt;/asp:Label&gt;
                              &lt;/td&gt;
                          &lt;/tr&gt;
                          &lt;tr&gt;
                              &lt;td colspan="4"&gt;
                                  &lt;asp:Label ID="titleLbl" runat="server" Font-Size="Large" ForeColor="Red" 
                                      Text="Label"&gt;&lt;/asp:Label&gt;
                              &lt;/td&gt;
                          &lt;/tr&gt;
                          &lt;tr&gt;
                              &lt;td colspan="4"&gt;
                                  &lt;asp:GridView ID="appForVacGrid" runat="server" AutoGenerateColumns="False" 
                                  CellPadding="4" ForeColor="#333333" AllowPaging="True" 
                                      onpageindexchanging="appForVacGrid_PageIndexChanging" GridLines="None" 
                                      PageSize="3"&gt;
                                      &lt;RowStyle BackColor="#EFF3FB" /&gt;
                                      &lt;Columns&gt;
                                          &lt;asp:TemplateField&gt;
                                              &lt;HeaderTemplate&gt;
                                                  App.ID
                                              &lt;/HeaderTemplate&gt;
                                              &lt;ItemTemplate&gt;
                                                  &lt;asp:Label ID="appIdLbl" runat="server" Text='&lt;%# Eval("AppId") %&gt;'&gt;&lt;/asp:Label&gt;
                                              &lt;/ItemTemplate&gt;
                                          &lt;/asp:TemplateField&gt;
                                          &lt;asp:TemplateField&gt;
                                              &lt;HeaderTemplate&gt;
                                                  First Name
                                              &lt;/HeaderTemplate&gt;
                                              &lt;ItemTemplate&gt;
                                                  &lt;asp:Label ID="firstNameLbl" runat="server" Text='&lt;%# Eval("AppFirstName") %&gt;'&gt;&lt;/asp:Label&gt;
                                              &lt;/ItemTemplate&gt;
                                          &lt;/asp:TemplateField&gt;
                                          &lt;asp:TemplateField&gt;
                                              &lt;HeaderTemplate&gt;
                                                  Last Name
                                              &lt;/HeaderTemplate&gt;
                                              &lt;ItemTemplate&gt;
                                                  &lt;asp:Label ID="lastNameLbl" runat="server" Text='&lt;%# Eval("AppLastName") %&gt;'&gt;&lt;/asp:Label&gt;
                                              &lt;/ItemTemplate&gt;
                                          &lt;/asp:TemplateField&gt;
                                          &lt;asp:TemplateField&gt;
                                              &lt;HeaderTemplate&gt;
                                                  Qualification
                                              &lt;/HeaderTemplate&gt;
                                              &lt;ItemTemplate&gt;
                                                  &lt;asp:Label ID="qualiNameLbl" runat="server" Text='&lt;%# Eval("QualiName") %&gt;'&gt;&lt;/asp:Label&gt;
                                              &lt;/ItemTemplate&gt;
                                          &lt;/asp:TemplateField&gt;
                                          &lt;asp:TemplateField&gt;
                                              &lt;HeaderTemplate&gt;
                                                  &amp;nbsp;Experience
                                              &lt;/HeaderTemplate&gt;
                                              &lt;ItemTemplate&gt;
                                                  &lt;asp:Label ID="Label1" runat="server" Text='&lt;%# Eval("TotalExpYear") %&gt;'&gt;&lt;/asp:Label&gt;
                                              &lt;/ItemTemplate&gt;
                                          &lt;/asp:TemplateField&gt;
                                          &lt;asp:TemplateField&gt;
                                              &lt;HeaderTemplate&gt;
                                                  EmailId
                                              &lt;/HeaderTemplate&gt;
                                              &lt;ItemTemplate&gt;
                                                  &lt;asp:Label ID="emailLbl" runat="server" Text='&lt;%# Eval("AppEmailId1") %&gt;'&gt;&lt;/asp:Label&gt;
                                              &lt;/ItemTemplate&gt;
                                          &lt;/asp:TemplateField&gt;
                                          &lt;asp:TemplateField&gt;
                                              &lt;HeaderTemplate&gt;
                                                  Send Mail
                                              &lt;/HeaderTemplate&gt;
                                              &lt;ItemTemplate&gt;
                                                  &lt;asp:CheckBox ID="sendMailBox" runat="server" /&gt;
                                              &lt;/ItemTemplate&gt;
                                          &lt;/asp:TemplateField&gt;
                                      &lt;/Columns&gt;
                                      &lt;FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" 
                                          HorizontalAlign="Right" /&gt;
                                      &lt;PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" 
                                          VerticalAlign="Top" /&gt;
                                      &lt;SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /&gt;
                                      &lt;HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" 
                                          Font-Size="Medium" HorizontalAlign="Left" /&gt;
                                      &lt;EditRowStyle BackColor="#2461BF" /&gt;
                                      &lt;AlternatingRowStyle BackColor="White" /&gt;
                                  &lt;/asp:GridView&gt;
                              &lt;/td&gt;
                          &lt;/tr&gt;
                          &lt;tr&gt;
                              &lt;td colspan="4" align="center"&gt;
                                  &amp;nbsp;
                              &lt;/td&gt;
                          &lt;/tr&gt;
                          &lt;tr&gt;
                              &lt;td align="center" colspan="4"&gt;
                                  &lt;asp:Button ID="sendMailBtn" runat="server" Height="40px" Text="SEND MAIL" 
                                      Width="100px" onclick="sendMailBtn_Click" /&gt;
                              &lt;/td&gt;
                          &lt;/tr&gt;
                          &lt;tr&gt;
                              &lt;%--&lt;td&gt;
                                  &amp;nbsp;
                              &lt;/td&gt;--%&gt;
                          &lt;/tr&gt;
                      &lt;/table&gt;
                  &lt;/ContentTemplate&gt;
                  &lt;Triggers&gt;
                  &lt;asp:AsyncPostBackTrigger ControlID="companyList" EventName="SelectedIndexChanged" /&gt;
                  &lt;asp:AsyncPostBackTrigger ControlID="deptList" EventName="SelectedIndexChanged" /&gt;
                  &lt;asp:AsyncPostBackTrigger ControlID="appForVacGrid" EventName="pageindexchanged" /&gt;
                  &lt;/Triggers&gt;
                &lt;/asp:UpdatePanel&gt;
  
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;
              &amp;nbsp;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;
              &amp;nbsp;
          &lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  
  &lt;script type="text/javascript"&gt;
      function alertOnBadSelection() {
          var select = document.getElementById('companyList');
          if (select.options[select.selectedIndex].value == "-Select Company-") {
              alert('Please Select Company!');
              return false;
          }
      }    
  &lt;/script&gt;
  </code></pre>
  
  <p>
  // This is code of my aspx.cs page</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Data;
  using System.Collections;
  
  public partial class HR_Department_searcAppForVac : System.Web.UI.Page
  {
  DataOperation oDo = new DataOperation();
  AppVacancyDetailClass objAppVacDetail = new AppVacancyDetailClass();
  protected void Page_Load(object sender, EventArgs e)
  {
      notifyLbl.Visible = false;
      titleLbl.Visible = false;
      sendMailBtn.Visible = false;
      try
      {
          if (!IsPostBack)
          {
              // Disable department dropdown list and vacancy dropdown list till company is not selected.
              deptList.Enabled = false;
              vacanyList.Enabled = false;
              //Fill Company dropdown list.
              DataTable objCmpnyTable = oDo.DropDownList("select * from tblCompanyMaster");
              if (objCmpnyTable.Rows.Count &gt; 0)
              {
                  companyList.DataSource = objCmpnyTable;
                  companyList.DataValueField = "CompId";
                  companyList.DataTextField = "CompName";
                  companyList.DataBind();
              }
              else
              {
                  notifyLbl.Visible = true;
                  notifyLbl.Text = "There is not any company in the list.";
              }
          }
          else
          {
              if (companyList.SelectedIndex &lt;= 0)
              {
                  //Disable department dropdown list and vacancy dropdown list till company is not selected.
                  deptList.Enabled = false;
                  vacanyList.Enabled = false;
              }
          }
       }
       catch (Exception)
       {
            throw;
       }
   }
  protected void companyList_SelectedIndexChanged(object sender, EventArgs e)
  {
      //Disable vacancy dropdown list till depratment list is not selected
      vacanyList.Enabled = false;
      appForVacGrid.DataSource = null;
      appForVacGrid.DataBind();
      try
      {
          if (companyList.SelectedIndex &gt; 0)
          {
              deptList.Enabled = true;
              deptList.Items.Clear();
              string str = "select * from vwCompWiseList where CompId=" + companyList.SelectedValue;
              DataTable objDeptTable = oDo.DropDownList("select DeptId,DeptName from vwCompWiseDept where CompId= "+companyList.SelectedValue);
              if (objDeptTable.Rows.Count &gt; 0)
              {
                  deptList.DataSource = objDeptTable;
                  deptList.DataTextField = "DeptName";
                  deptList.DataValueField = "deptId";
                  deptList.DataBind();
                  deptList.Items.Insert(0, new ListItem("--Select Department--", "-1"));
              }
              else 
              {
                  deptList.Items.Insert(0, new ListItem("--No Departments--", "-1"));
                  notifyLbl.Visible = true;
                  notifyLbl.Text = "No Departments Available in " + companyList.SelectedItem.Text;
              }
          }
          else
          {
              notifyLbl.Visible = true;
              notifyLbl.Text = "Select Company....";
              appForVacGrid.DataSource = null;
              appForVacGrid.DataBind();
          }
      }
      catch (Exception)
      {
  
          throw;
      }    
  }
  protected void deptList_SelectedIndexChanged(object sender, EventArgs e)
  {
      try
      {
          if (deptList.SelectedIndex &gt; 0)
          {
              vacanyList.Enabled = true;
              vacanyList.Items.Clear();
              DataTable objVacancytbl = oDo.DropDownList("select VacId,VacTitle from tblVacancyMaster where DeptId =" + deptList.SelectedValue + " and CompId=" + companyList.SelectedValue);
              if (objVacancytbl.Rows.Count &gt; 0)
              {
                  vacanyList.DataSource = objVacancytbl;
                  vacanyList.DataValueField = "VacId";
                  vacanyList.DataTextField = "VacTitle";
                  vacanyList.DataBind();
                  vacanyList.Items.Insert(0, new ListItem("--Select Vacancy--", "-1"));
                  appForVacGrid.DataSource = null;
                  appForVacGrid.DataBind();
              }
              else
              {
                  notifyLbl.Visible = true;
                  notifyLbl.Text = "ALL VACANCIES ARE CLOSED IN "+" "+deptList.SelectedItem.Text.ToUpper();
                  vacanyList.Enabled = false;
                  appForVacGrid.DataSource = null;
                  appForVacGrid.DataBind();
              }
          }
          else
          {
              notifyLbl.Visible = true;
              notifyLbl.Text = "Select Department...";
              appForVacGrid.DataSource = null;
              appForVacGrid.DataBind();
              vacanyList.Enabled = false;
          }
      }
      catch (Exception)
      {            
          throw;
      }
  }
  protected void vacanyList_SelectedIndexChanged(object sender, EventArgs e)
  {
      try
      {
          DataTable AppListTbl = objAppVacDetail.GetValue("CompId=" + companyList.SelectedValue + " and DeptId=" + deptList.SelectedValue + " and VacId=" + vacanyList.SelectedValue);
          if (AppListTbl.Rows.Count &gt; 0)
          {
              appForVacGrid.DataSource = AppListTbl;
              appForVacGrid.DataBind();
              appForVacGrid.Columns[5].Visible = false;
              Session.Add("snAppListTbl", AppListTbl);
              titleLbl.Visible = true;
              titleLbl.Text = AppListTbl.Rows.Count.ToString() + " " + "CANDIDATE(S) ARE ELIGIBLE FOR THE POST OF" + " " + vacanyList.SelectedItem.Text.ToUpper() + ".";
              sendMailBtn.Visible = true;
          }
          else
          {
              notifyLbl.Visible = true;
              notifyLbl.Text = "ALL VACANCIES ARE CLOSED IN " + " " + deptList.SelectedItem.Text.ToUpper();
              appForVacGrid.DataSource = null;
              appForVacGrid.DataBind();
          }
      }
      catch (Exception)
      {
  
          throw;
      }
  }
  protected void appForVacGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
  {
      //DataTable AppListTbl = (DataTable)Session["snAppListTbl"];
      titleLbl.Visible = true;
      titleLbl.Text = ((DataTable)Session["snAppListTbl"]).Rows.Count.ToString() + " " + "CANDIDATE(S) ARE ELIGIBLE FOR THE POST OF" + " " + vacanyList.SelectedItem.Text.ToUpper() + ".";
      appForVacGrid.PageIndex = e.NewPageIndex;
      appForVacGrid.DataSource = (DataTable)Session["snAppListTbl"];
      appForVacGrid.DataBind();
      sendMailBtn.Visible = true;
  }
  protected void sendMailBtn_Click(object sender, EventArgs e)
  {
      DataTable AppListTable = ((DataTable)Session["snAppListTbl"]);
      int intTotalRows = AppListTable.Rows.Count;
      string strFromId="",strToId="",strCcId="",strBccId="";
      string strVacTitle="",strCompName="",strMailBody="";
      string strSubject = "Rgarding Selection of Your Resume";
      Label EmailLbl;
      //for (int intRow = 0; intRow &lt; intTotalRows; intRow++)
      foreach (GridViewRow Row in appForVacGrid.Rows)
      {
          CheckBox objSendMail = (CheckBox)Row.FindControl("sendMailBox");
          //CheckBox objSendMail = (CheckBox)appForVacGrid.Rows[Row].FindControl("sendMailBox");
          if (objSendMail.Checked)
          {
              if (strToId == "")
              {
                  //strToId = AppListTable.Rows[Row]["AppEmailId1"].ToString();
                  EmailLbl = (Label)Row.FindControl("emailLbl");
                  strToId = EmailLbl.Text;
              }
              else
              {
                  //strToId += "," + AppListTable.Rows[Row]["AppEmailId1"].ToString();
                  EmailLbl = (Label)Row.FindControl("emailLbl");
                  strToId +=","+ EmailLbl.Text;
              }
          }
      }
      strVacTitle = AppListTable.Rows[intTotalRows]["VacTitle"].ToString();
      strCompName = AppListTable.Rows[intTotalRows]["CompName"].ToString();
      strMailBody = CommonProcedures.GetMailBody(strVacTitle, strCompName);
      //CommonProcedures.SendMail(strFromId, strToId, strCcId, strBccId, strSubject, null, strMailBody, false);
  }
  }
  </code></pre>
  
  <p>Now I want to send mail to all those candidates whose checkbox is checked..
  I trid so many things but didnt get any solution</p>