ASP.NET GridView Tutorial
Lesson 261 of 301 · Implement default paging in an asp.net gridview without using datasource controls – Part 53 · Database
Implement default paging in an asp.net gridview without using datasource controls – Part 53
Link for csharp, asp.net, ado.net, dotnet basics and sql server video tutorial playlists
http://www.youtube.com/user/kudvenkat/playlists
Link for text version of this video
http://csharp-video-tutorials.blogspot.com/2013/04/implement-default-paging-in-aspnet_10.html
In this video we will discuss about implementing default paging in a gridview control that does not use any data source controls.
Step 1: Add a class file with name = “EmployeeDataAccessLayer.cs”.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace Demo
{
public class Employee
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string City { get; set; }
}
public class EmployeeDataAccessLayer
{
// Replace square brackets with angular brackets
public static List[Employee] GetAllEmployees()
{
// Replace square brackets with angular brackets
List[Employee] listEmployees = new List[Employee]();
string CS = ConfigurationManager.ConnectionStrings[“DBCS”].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand(“Select * from tblEmployee”, con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Employee employee = new Employee();
employee.EmployeeId = Convert.ToInt32(rdr[“EmployeeId”]);
employee.Name = rdr[“Name”].ToString();
employee.Gender = rdr[“Gender”].ToString();
employee.City = rdr[“City”].ToString();
listEmployees.Add(employee);
}
}
return listEmployees;
}
}
}
Step 2: Drag and drop a gridview control on webform1.aspx and set the following properties of GridView1 control
AllowPaging=”true”
PageSize=”3″
Step 3: Generate event handler method for PageIndexChanging event of GridView control.
Step 4: Copy and paste the following code in webform1.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = EmployeeDataAccessLayer.GetAllEmployees();
GridView1.DataBind();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = EmployeeDataAccessLayer.GetAllEmployees();
GridView1.DataBind();
}