分页查询

基本过程:
DAL层:
1.在dal层传递页码、和每一页多少量,以及多少总数
/public List<Student> GetStudentListByPage(int pageIndex,int pageSize,ref int count)//只传递参数count
//{
//注释:sql命令 创建一个先查询所有的行的sid从小到大的tep表,之后用查出来得sid去制定条件页码=页码*所有数-(所有数-1)并且页数等于页码*所有数
// string sql = "select * from (select ROW_NUMBER() over(order by Id) as sid,* from Students ) temp where sid>=@pageIndex*@pageSize-(@pageSize-1) and sid<=@pageIndex*@pageSize";
// List<SqlParameter> paras = new List<SqlParameter>();
// paras.Add(new SqlParameter("@pageIndex", pageIndex));
// paras.Add(new SqlParameter("@pageSize", pageSize));
// DataTable dt= SqlHelper.ExecuteTable(sql, paras);
// List<Student> students = new List<Student>();
// for (int i = 0; i < dt.Rows.Count; i++)
// {
// Student s = new Student();
// s.Id = Convert.ToInt32(dt.Rows[i]["Id"]);
// s.Name = dt.Rows[i]["Name"].ToString();
// s.Sno = dt.Rows[i]["Sno"].ToString();
// s.Sex = dt.Rows[i]["Sex"].ToString();
// s.Age = Convert.ToInt32(dt.Rows[i]["Age"]);
// s.GradeId = Convert.ToInt32(dt.Rows[i]["GradeId"]);
// students.Add(s);
// }
// sql = "select COUNT(*) from Students";//所有的总和
// count=Convert.ToInt32( SqlHelper.ExecuteScalar(sql, null));//查询所有学生数
// return students;
//}
BLL层:
/分页获取学生列表
//public List<Student> GetStudentListByPage(int pageIndex, int pageSize,ref int count)//传递参数
//{
// return dao.GetStudentListByPage(pageIndex, pageSize,ref count);//传递给dal层
//}

UI层:
//页码与参数全局变量
private int pageIndex = 1;
private int pageSize = 10;
private int count;

//显示所有学生信息
private void FrmStudent_Load(object sender, EventArgs e)
{
this.btnPrev.Enabled = false;
this.ShowStudent();
}
public void ShowStudent()
{
List<Student> students = ss.GetStudentList();
dgvStudents.DataSource = new BindingList<Student>(students);
}

//分页学生信息
private void button5_Click(object sender, EventArgs e)
{ //条件判断 每次点一次页码增加一次,并且通过条件得到是否为空,是则按钮 被禁止、否则继续
this.pageIndex++;
if (pageIndex == (count % pageSize == 0 ? count / pageSize : count / pageSize + 1))
{
btnNext.Enabled = false;
}
btnPrev.Enabled = true;
this.ShowStudent();
}

private void button4_Click(object sender, EventArgs e)
{
this.pageIndex--;
btnNext.Enabled = true;
if (pageIndex == 1)
{
btnPrev.Enabled = false;
}
this.ShowStudent();
}
}
}

posted @ 2016-02-19 11:03  西瓜冰镇老板  阅读(209)  评论(0编辑  收藏  举报