Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Implement default paging in an asp.net gridview without using datasource controls - Part 53

Suggested Videos 
Part 50 - Implement paging in an asp.net gridview that uses sqldatasource
Part 51 - Implement default paging gridview that uses objectdatasource
Part 52 - Implement custom paging in gridview that uses objectdatasource



In this video we will discuss about implementing default paging in a gridview control that does not use any data source controls.

We will be using tblEmployee table for this demo. Please refer to Part 13 by clicking here, if you need the sql script to create and populate this table.



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
    {
        public static List<Employee> GetAllEmployees()
        {
            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.

At this point, the HTML of "WebForm1.aspx" should be as shown below.
<asp:GridView 
    ID="GridView1" runat="server" 
    AllowPaging="true" PageSize="3" 
    onpageindexchanging="GridView1_PageIndexChanging">
</asp:GridView>

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();
}

No comments:

Post a Comment

It would be great if you can help share these free resources