Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to apply paging on a list

Tags:

I have a function that retrieves data from database and add it into list. My list is ready and shows the data but i want paging on that list so that it shows limited records per page. but no idea about how to do that.
Here is my code of that data layer function.

 public List<demodto> executereader(List<demodto> Ldemo,SqlCommand cmdshow, string tablename)     {          SqlConnection cn;          try          {              cn = this.getconnection();               cmdshow.Connection = cn;              cn.Open();               SqlDataReader rd = cmdshow.ExecuteReader();              while (rd.Read())              {                  demodto dtoobj1 = new demodto();                  dtoobj1.ID = Convert.ToInt32(rd[0].ToString());                  dtoobj1.Name = rd[1].ToString();                  dtoobj1.PhNo = Convert.ToInt32(rd[2].ToString());                  dtoobj1.Address = rd[3].ToString();                  dtoobj1.Gender = rd[4].ToString();                  dtoobj1.Email = rd[5].ToString();                  dtoobj1.Emptype = rd[6].ToString();                   Ldemo.Add(dtoobj1);               }              cn.Close();              return Ldemo;          }          catch (Exception ex2)          {              throw new DataException("error....." + ex2.Message);           }      } 

And this is for DTO class..

 public class demodto {     public Int32 ID{get;set;}     public string Name{get;set;}     public Int32 PhNo { get; set; }     public string Address{get;set;}     public string Gender { get; set; }     public string Email { get; set; }     public string Emptype { get; set; }  } 

please help me. Thanks.

like image 478
vidhi Avatar asked Jan 26 '14 10:01

vidhi


People also ask

What is paging in C#?

The C# pagination logic is contained in a single Pager class that takes the following constructor arguments: totalItems (required) - the total number of items to be paged. currentPage (optional) - the current active page, defaults to the first page. pageSize (optional) - the number of items per page, defaults to 10.

What is paging in asp net?

We use C# code to bind the SQL data with a GridView control and use the following simple steps to make your ASP.NET GridView control with paging enabled. The GridView control provides you with an easy way to display the number of items on a page without taking much space, with the help of paging.

How many items can a list hold C#?

MaxValue or 2,147,483,647 is the most items you could stick in a list.


Video Answer


1 Answers

You can page a list with LINQ, like this:

IList<demodto> GetPage(IList<demodto> list, int page, int pageSize) {     return list.Skip(page*pageSize).Take(pageSize).ToList(); } 

For example, suppose each page has 50 records. To get a third page, call

IList<demodto> thirdPage = GetPage(dataList, 3, 50); 

Note, however, that applying paging to data in memory makes very little sense: the idea behind paging is to cut down on the time it takes to retrieve your data from the database, and to save some memory by keeping only a single page, which is not going to happen in your case, because all data is retrieved at once.

In order to make paging worth the effort, you need to move it into the database. Change your method to accept page size and number, and use them to change the SQL to retrieve the list for a single page. Don't forget to force ordering on your sql read, otherwise the same data might appear on different pages. Your SQL needs to be modified to support pagination. This is done differently depending on your database. MS SQL Server solution is described in this answer.

like image 151
Sergey Kalinichenko Avatar answered Oct 13 '22 21:10

Sergey Kalinichenko