CSwinform程序分页
2021-01-13 23:13
标签:cte page final btn eve exec mamicode add rds 带参数的分装: 存储过程的分页: CSwinform程序分页 标签:cte page final btn eve exec mamicode add rds 原文地址:https://www.cnblogs.com/sunliyuan/p/12274271.html///
public partial class FrmDataPager : Form
{
private SqlDataPager objPager = null;
public FrmDataPager()
{
InitializeComponent();
this.dtpBirthday.Text = "1988-1-1";
//初始化数据分页对象
objPager = new SqlDataPager()
{
PrimaryKey = "StudentId",//表的主键
TableName = "Students",
FiledName = "StudentId,StudentName,Gender,Birthday,PhoneNumber",
CurrentPage = 1,
Sort = "StudentId ASC"//排序条件
};
//设置默认的显示条数
this.cboRecordList.SelectedIndex = 1;
this.dgvStudentList.AutoGenerateColumns = false;
//禁用相关按钮
this.btnFirst.Enabled = false;
this.btnNext.Enabled = false;
this.btnPre.Enabled = false;
this.btnLast.Enabled = false;
this.btnToPage.Enabled = false;
}
//执行查询的公共方法
private void Query()
{
//开启所有按钮
this.btnFirst.Enabled = true;
this.btnNext.Enabled = true;
this.btnPre.Enabled = true;
this.btnLast.Enabled = true;
this.btnToPage.Enabled = true;
//【1】设置分页查询的条件
objPager.Condition = string.Format("Birthday>‘{0}‘", this.dtpBirthday.Text);
//【2】设置每页显示的条数
objPager.PageSize = Convert.ToInt32(this.cboRecordList.Text.Trim());
//【3】执行查询
this.dgvStudentList.DataSource = objPager.GetPagedData();
//【4】显示记录总数、显示总页数、显示当前页码
this.lblRecordsCount.Text =objPager.RecordCount.ToString ();
this.lblPageCount.Text = objPager.TotalPages.ToString();
if (this.lblPageCount.Text == "0")
{
this.lblCurrentPage.Text = "0";
}
else
{
this.lblCurrentPage.Text = objPager.CurrentPage.ToString();
}
//禁用按钮的情况
if (this.lblPageCount.Text == "0" || this.lblPageCount.Text == "1")
{
this.btnFirst.Enabled = false;
this.btnNext.Enabled = false;
this.btnPre.Enabled = false;
this.btnLast.Enabled = false;
this.btnToPage.Enabled = false;
}
else
{
this.btnToPage.Enabled = true;
}
}
//提交查询
private void btnQuery_Click(object sender, EventArgs e)
{
objPager.CurrentPage = 1;//每次执行查询都要设置为第1页
Query();
this.btnPre.Enabled = false;
this.btnFirst.Enabled = false;
}
//第1页
private void btnFirst_Click(object sender, EventArgs e)
{
objPager.CurrentPage = 1;
Query();
this.btnPre.Enabled = false;
this.btnFirst.Enabled = false;
// btnQuery_Click(null, null);//可以直接调用上面的事件
}
//下一页
private void btnNext_Click(object sender, EventArgs e)
{
objPager.CurrentPage += 1;
Query();
//当执行到最后一页的时候应该禁用最后一页和下一页的按钮
if (objPager.CurrentPage == objPager.TotalPages)
{
this.btnNext.Enabled = false;
this.btnLast.Enabled = false;
}
}
//上一页
private void btnPre_Click(object sender, EventArgs e)
{
objPager.CurrentPage -= 1;
Query();
if (objPager.CurrentPage == 1)
{
this.btnPre.Enabled = false;
this.btnFirst.Enabled = false;
}
}
//最后一页
private void btnLast_Click(object sender, EventArgs e)
{
objPager.CurrentPage = objPager.TotalPages;
Query();
this.btnNext.Enabled = false;
this.btnLast.Enabled = false;
}
//跳转到
private void btnToPage_Click(object sender, EventArgs e)
{
if (this.txtToPage.Text.Trim().Length == 0)
{
MessageBox.Show("请输入要跳转的页码!","信息提示");
this.txtToPage.Focus();
return;
}
//使用正则表达式验证必须为大于0的正整数...
int toPage = Convert.ToInt32(this.txtToPage.Text.Trim());
if (toPage > objPager.TotalPages)
{
MessageBox.Show("跳转的页数不能大于数据总页数!","信息提示");
this.txtToPage.Focus();
this.txtToPage.SelectAll();
return;
}
//开始跳转页数
objPager.CurrentPage = toPage;
Query();
if (objPager.CurrentPage == 1)
{
this.btnPre.Enabled = false;
this.btnFirst.Enabled = false;
}
else if (objPager.CurrentPage == objPager.TotalPages)
{
this.btnNext.Enabled = false;
this.btnLast.Enabled = false;
}
}
//关闭窗口
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
public class SqlDataPager
{
public SqlDataPager() { }
///
private static string connString = "Server=.;DataBase=DataPagerDB;Uid=sa;Pwd=password01!";
///
use DataPagerDB
go
if exists(select * from sysobjects where name=‘usp_DataPager‘)
drop procedure usp_DataPager
go
create procedure usp_DataPager
@PageSize int,--每页显示多少条
@FilterCount int, --过滤的条数
@Birthday varchar(20) --查询条件参数
as
Select Top (@PageSize) StudentId,StudentName,Gender,Birthday,PhoneNumber from Students
where Birthday>@Birthday and StudentId not in
(Select Top (@FilterCount) StudentId from Students where Birthday>@Birthday order by StudentId ASC)
order by StudentId ASC
--查询满足记录条数
select count(*) from Students where Birthday>@Birthday
go
///
private static string connString = "Server=.;DataBase=DataPagerDB;Uid=sa;Pwd=password01!";
///