Hi Rezu2215,
To count GridView records and display in another page use Session.
For count GridView records you can either use GridView Rows count property or Entity set count.
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database Employees table that you can download using the link given below.
Download Northwind Database
HTML
Default
<table>
<tr>
<td>
<asp:GridView runat="server" ID="gvEmployees" AutoGenerateColumns="false" AllowPaging="true"
PageSize="5">
<Columns>
<asp:BoundField DataField="EmployeeID" HeaderText="Id" />
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
</td>
<td>
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false" AllowPaging="true"
PageSize="5">
<Columns>
<asp:BoundField DataField="EmployeeID" HeaderText="Id" />
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
</td>
</tr>
</table>
<br />
<asp:Button Text="Redirect" runat="server" PostBackUrl="~/Default1.aspx" />
Default1
GridView record count using Rows count is :
<asp:Label ID="lblCountRows" runat="server" />
<br />
GridView record count using Entity count is :
<asp:Label ID="lblCountEntity" runat="server" />
Code
C#
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
NorthwindModel.NorthwindEntities entities = new NorthwindModel.NorthwindEntities();
gvEmployees.DataSource = entities.Employees.ToList();
gvEmployees.DataBind();
// Using GridView Rows count property.
gvEmployees.AllowPaging = false;
gvEmployees.DataBind();
Session["RecordCountRows"] = gvEmployees.Rows.Count;
gvEmployees.AllowPaging = true;
gvEmployees.DataBind();
// Using Entity count.
GridView1.DataSource = entities.Employees.ToList();
GridView1.DataBind();
Session["RecordCountEntity"] = entities.Employees.Count();
}
}
Default1.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
lblCountRows.Text = Session["RecordCountRows"].ToString();
lblCountEntity.Text = Session["RecordCountEntity"].ToString();
}
VB.Net
Default.aspx.vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim entities As NorthwindModel.NorthwindEntities = New NorthwindModel.NorthwindEntities()
gvEmployees.DataSource = entities.Employees.ToList()
gvEmployees.DataBind()
' Using GridView Rows count property.
gvEmployees.AllowPaging = False
gvEmployees.DataBind()
Session("RecordCountRows") = gvEmployees.Rows.Count
gvEmployees.AllowPaging = True
gvEmployees.DataBind()
' Using Entity count.
GridView1.DataSource = entities.Employees.ToList()
GridView1.DataBind()
Session("RecordCountEntity") = entities.Employees.Count()
End If
End Sub
Default1.aspx.vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
lblCountRows.Text = Session("RecordCountRows").ToString()
lblCountEntity.Text = Session("RecordCountEntity").ToString()
End Sub
Screenshot
