Griview 分页功能实现 asp.net
分页效果图:
Griview 数据绑定:
private void hh()//gridview绑定数据
{
using (SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=StaffMs;Integrated Security=True"))
{
SqlCommand com = new SqlCommand("select * from(select
row_number()over(order by sf_number)as number ,*from dbo.staff)a where number between @a and @b", con);//取@a到@b的数据
int lab1 = int.Parse(this.lab1.Text);//la1.text初始值是1
int a = (lab1 - 1) * 10 + 1;
int b = lab1 * 10;
com.Parameters.Add(new SqlParameter("@a",a));
com.Parameters.Add(new SqlParameter("@b",b));//读取a到b之间的数据
SqlDataAdapter dr = new SqlDataAdapter(com);
DataSet dt = new DataSet();
dr.Fill(dt);
this.GridView1.DataKeyNames = new string[] { "sf_number" };//更新时需要用
this.GridView1.DataSource=dt.Tables[0];
this.GridView1.DataBind();
SqlCommand com1 = new SqlCommand("select count(*) from dbo.staff", con);//查询表中记录总数
con.Open();
int count = int.Parse(com1.ExecuteScalar().ToString());//返回第一行第一列的数据,也就是表中记录总数
int pagecount = count / 10;//数据分页,每十个数据分一页 !若想五个或六个数据的话,修改绿色背景处即可
if(pagecount%10!=0)
{
pagecount++;
lab3.Text = pagecount.ToString();
}
}
}
首页 上一页 1/1下一页 最后一页 :
protected void btnfirst_Click(object sender, EventArgs e)//首页
{
lab1.Text = "1";
hh();
}
protected void btnqian_Click(object sender, EventArgs e)//上一页
{
int jian = int.Parse(lab1.Text);
jian--;
if (jian>=1)
{
lab1.Text = jian.ToString();
}
hh();
}
protected void btnhou_Click(object sender, EventArgs e)//下一页
{
int jia = int.Parse(lab1.Text);
jia++;
if(jia<=int.Parse(lab3.Text))
{
lab1.Text = jia.ToString();
}
hh();
}
protected void btnlast_Click(object sender, EventArgs e)//最后一页
{
lab1.Text =lab3.Text;
hh();
}