관리 메뉴

IT 쟁이

DataGrid4 - 사용자 정의 폐이징 만들기 - 출처 - Taeyo ASP.NET with C# 본문

ASP.NET/서버컨트롤

DataGrid4 - 사용자 정의 폐이징 만들기 - 출처 - Taeyo ASP.NET with C#

클라인STR 2008. 1. 16. 18:47

DataGrid에서의 사용자 정의 폐이징

이유 ? 자동폐이징 방식에서는 레코드의 수가 많아 지면 부하가 발생한다.
효과적으로 폐이징하기 위해서는 전체 레코드 중에서 폐이지에 해당하는 5개의 레코드만을 데이터베이스로 부터 가져오기만 하면 된다.

SELECT TOP 5 title_id, title, price from titles where title_id not in (select top [현재 페이지 인덱스 * 5] title_id from titles order by title_id) order by title_id


이 구문 외에도 같은 결과를 얻을 수 있는 수많은 SELECT 구문들이  존재하지만 이 구문을 이용하였다.


(select top [현재 페이지 인덱스 * 5] title_id from titles order by title_id)

이 구문의 뜻은 top 구문을 사용하여 현재 폐이지 인덱스 값에 5를 곱한  레코드의 값을 얻는다.

예를들어 사용자가 3폐이지를 선택했따면 총 15개의 레코드를 반환할것이다.


15개의 레코드의 반환값이 A,B,C,D,E,F,G,H,I,J,K,L,M,N,O 라는 데이터를 얻게 된다고하면

SELECT TOP 5 title_id, title, price from titles where title_id not in (A,B,C,D,E,F,G,H,I,J,K,L,M,N,O) order by title_id
이렇게 될것이다.
NOT IN 의 조건은 AND와 같다. 해당조건의 만족하지 않은 5개의 title_id  반환할 것이다.

만약
사용자가 요청한 페이지가 1페이지 즉 페이지 인덱스가 0이라면 실제로 어떠한 데이터도 존재하지 않는다.
최상의 레코드 5개를 얻게 된다.

웹폼을 추가하고 웹폼위에 DataGrid 컨트롤을 하나 올리고 자동서식에서 전문가 2를 선택한다.


private int iPage=1;
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 여기에 사용자 코드를 배치하여 페이지를 초기화합니다.
   if(!IsPostBack)
   {
   
this.DataGrid1.PageSize=5;
    this.DataGrid1.VirtualItemCount = this.GetTotalRecordCount();

    BindData();
   }

  }

private  void BindData()
  {
   string query ="Server=(local); database=Pubs; user id=sa; pwd=1";
   SqlConnection con = new SqlConnection(query);
   string sql = " select top 5 title_id, title, price, pubdate from titles " +
    " where title_id not in " +
    "(select top " + iPage * 5 + "title_id from titles order by title_id)" +
    " order by title_id";

   SqlDataAdapter adp = new SqlDataAdapter(sql,con);

   DataSet ds = new DataSet();
   adp.Fill(ds, "titles");

   this.DataGrid1.DataSource = ds.Tables["titles"];
   this.DataGrid1.DataBind();
  }



다음 코드를 작성한다. 현재 페이지에 해당하는 변수로 iPage를 선언한다. iPage는 현재 출력할 페이지의 인덱스를 나타낸다. 1페이지에 경우 그 값이 0이어야 하고 2페이지인 경우 그 값이 1이어야 한다. 기본적으로 처음 출력시에는 첫 페이지가 출력되어야 할 것이므로, iPage 초기값은 0으로 주었다.

사용자 정의 페이징은 이처럼 하나부터 열까지 모두를 여러분이 직접 코드로써 작성해야만 한다.
페이징을 하기 위해서는 한페이지당 보여줄 크기와 전체 레코드의 수(VirtualItemCount)가 필요한다.

private int GetTotalRecordCount()
  {
   string query = "Server=(local); database=pubs; user id=sa; pwd=1;";
   SqlConnection con = new SqlConnection(query);
   string sql = "SELECT Count(*) from titles";
   SqlCommand cmd = new SqlCommand(sql,con);
   con.Open();
   int totalCount = int.Parse(cmd.ExecuteScalar().ToString());
   con.Close();

   return totalCount;
  }

DataGrid의 속성중 AllowPaging 속성과 AllowCustomPaging 속성을 True 로 변경한다.

this.DataGrid1.PageSize=5;
this.DataGrid1.VirtualItemCount = this.GetTotalRecordCount();

다음 코드를 Page_Load 부분에 추가한다.

데이터그리드의 PageIndexChanged 이벤트를 추가한다.

private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
  {
   iPage = e.NewPageIndex;
   this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
   this.BindData();
  }

폐이징의 출력부의 스타일을 변경하고 싶다면 다음처럼 <PageStyle> 부분의 HorizontalAlign 속성을 변경한다.


사용자 삽입 이미지

Comments