「asp.net」GridViewでページング機能を実現する

asp.netコード:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class GridView_Page : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//ページネーション画像ボタンを設定
if (!IsPostBack)
{
GridView1.Caption = “これはGridViewのアプリケーション";
//Captionプロパティ
GridView1.PagerSettings.Mode = PagerButtons.NextPreviousFirstLast;
GridView1.PagerSettings.NextPageImageUrl = “img/next.gif";
GridView1.PagerSettings.PreviousPageImageUrl = “img/pre.gif";
GridView1.PagerSettings.FirstPageImageUrl = “img/first.gif";
GridView1.PagerSettings.LastPageImageUrl = “img/last.gif";
GridView1.PageSize = 10; //ページごとに最も10個記録を表示;
BindData();
}
}
private void BindData()
{
//GridViewの中でデータを配置
string Constr = “server=localhost; uid=sa;pwd=startnews24;database=NorthWind";
string sqlstr = “select * from products";
SqlConnection con = new SqlConnection(Constr);
SqlDataAdapter ad = new SqlDataAdapter(sqlstr, con);
DataSet ds = new DataSet();
ad.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_PageIndexChanged(object sender, EventArgs e)
{
//ページに分けた後に改めてデータを配置
BindData();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//ページング完了前
GridView1.PageIndex = e.NewPageIndex;
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
//サブページ表示の追加
GridViewRow bottomPagerRow = GridView1.BottomPagerRow;
Label bottomLabel = new Label();
bottomLabel.Text = “現在ページングの位置:(" + (GridView1.PageIndex + 1) + “/" + GridView1.PageCount + “)";
bottomPagerRow.Cells[0].Controls.Add(bottomLabel);
}
}

Development

Posted by arkgame