C/S中DataGrid数据分页显示
ZD_SSGL_PublicCode PublicCode = new ZD_SSGL_PublicCode();DataSet myds = new DataSet();
DataSet myds1 = new DataSet();
int pagecount = 3;
int page = 0;
int pagedq = 1;
private void GLQSXX_Load(object sender, EventArgs e)
{
this.dataGrid2.ReadOnly = true;
string sqlselect = "select top 3 * from TBQS";
DataSet ds = new DataSet();
ds = PublicCode.GetMySet(sqlselect);
this.dataGrid2.DataSource = ds.Tables[0].DefaultView;
int count = Convert.ToInt32(PublicCode.GetString("select count(*) from TBQS"));
if (count <= 3)
{
page = 1;
pagecount = count;
}
else if (count % 3 != 0)
{
page = count / 3 + 1;
pagecount = count % 3;
}
else
{
page = count / 3 ;
pagecount = 3;
}
pagesm.Text = "当前为:第1页,共" + page + "页。";
myds = ds;
}
private void bt_goto_Click(object sender, EventArgs e)
{
int count = 0;
DataSet ds = new DataSet();
string sql = "select count(*) from TBQ" ;
try
{
count = Convert.ToInt32(PublicCode.GetString(sql));
}
catch (Exception ex)
{
count=0;
}
int tb_pagedq=0;
if (checkamount(tb_page.Text))
{
tb_pagedq = Convert.ToInt32(tb_page.Text);
}
if (tb_pagedq <= page)
{
string sqlselect = "";
if (tb_pagedq > 1)
{
sqlselect = "select top " + (tb_pagedq - 1) * 3 + " QS_H as 寝室号,SS_DM as 宿舍代码,QS_SX as 寝室属性,RZRY_SX as 入住人员属性,"
+ "QSDH as 寝室电话,QSZTBS as 寝室状态标识,CWS as 床位数,BZ as 备注 from TBQS";
try
{
ds = PublicCode.GetMySet(sqlselect);
DataRow dr = ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1];
string qsh = dr[0].ToString();
ds.Dispose();
sqlselect = sqlselect = "select top 3 QS_H as 寝室号,SS_DM as 宿舍代码,QS_SX as 寝室属性,RZRY_SX as 入住人员属性,"
+ "QSDH as 寝室电话,QSZTBS as 寝室状态标识,CWS as 床位数,BZ as 备注 from TBQS where qs_h>'" + qsh + "' order by QS_H";
ds = PublicCode.GetMySet(sqlselect);
dataGrid2.DataSource = ds.Tables[0].DefaultView;
pagesm.Text = "当前为:第" + tb_pagedq + "页,共" + page + "页。";
}
catch (Exception ex)
{
MessageBox.Show("没有你要的页的数据。");
}
}
else if (tb_pagedq == 1)
{
sqlselect = "select top 3 QS_H as 寝室号,SS_DM as 宿舍代码,QS_SX as 寝室属性,RZRY_SX as 入住人员属性,"
+ "QSDH as 寝室电话,QSZTBS as 寝室状态标识,CWS as 床位数,BZ as 备注 from TBQS where order by QS_H";
ds = PublicCode.GetMySet(sqlselect);
dataGrid2.DataSource = ds.Tables[0].DefaultView;
pagesm.Text = "当前为:第1页,共" + page + "页。";
}
else
{
MessageBox.Show("没有你要的页的数据。");
}
}
else
{
MessageBox.Show("没有你要的页的数据。");
}
}
private void NavigateTopPage(object sender, System.EventArgs e)
{
if (dataGrid2.DataSource == null)
{
return;
}
string pageinfo = ((Label)sender).Name;
string sqlselect = "select top 3 QS_H as 寝室号,SS_DM as 宿舍代码,QS_SX as 寝室属性,RZRY_SX as 入住人员属性,"
+ "QSDH as 寝室电话,QSZTBS as 寝室状态标识,CWS as 床位数,BZ as 备注 from TBQS";
if (myds.Tables[0].Rows.Count>0)
{
myds1 = myds;
}
DataRow frist, last;
frist = myds1.Tables[0].Rows[0];
string fristqs_h = frist[0].ToString();
last = myds1.Tables[0].Rows[myds1.Tables[0].Rows.Count - 1];
string lastqs_h = last[0].ToString();
string qs_h = "";
if(string.Compare(fristqs_h,lastqs_h)==1)
{
qs_h = fristqs_h;
fristqs_h = lastqs_h;
lastqs_h = qs_h;
}
if (pagecount % 3 != 0)
{
pagecount = pagecount % 3;
}
else
{
pagecount = 3;
}
switch (pageinfo)
{
case "frist":
sqlselect = "select top 3 QS_H as 寝室号,SS_DM as 宿舍代码,QS_SX as 寝室属性,RZRY_SX as 入住人员属性,"
+ "QSDH as 寝室电话,QSZTBS as 寝室状态标识,CWS as 床位数,BZ as 备注 from TBQS order by QS_H";
pagesm.Text = "当前为:第1页,共" + page + "页。";
pagedq = 1;
break;
case "last":
sqlselect = "select top "+pagecount+" QS_H as 寝室号,SS_DM as 宿舍代码,QS_SX as 寝室属性,RZRY_SX as 入住人员属性,"
+ "QSDH as 寝室电话,QSZTBS as 寝室状态标识,CWS as 床位数,BZ as 备注 from TBQS order by QS_H desc";
pagesm.Text = "当前为:第"+page+"页,共" + page + "页。";
pagedq = page;
break;
case "up":
sqlselect = "select top 3 QS_H as 寝室号,SS_DM as 宿舍代码,QS_SX as 寝室属性,RZRY_SX as 入住人员属性,"
+ "QSDH as 寝室电话,QSZTBS as 寝室状态标识,CWS as 床位数,BZ as 备注 from TBQS where qs_h< '" + fristqs_h + "' order by QS_H desc";
if (pagedq == 1)
{
pagesm.Text = "当前为:第1页,共" + page + "页。";
}
else
{
pagesm.Text = "当前为:第" + (--pagedq) + "页,共" + page + "页。";
}
break;
case "down":
sqlselect = "select top 3 QS_H as 寝室号,SS_DM as 宿舍代码,QS_SX as 寝室属性,RZRY_SX as 入住人员属性,"
+ "QSDH as 寝室电话,QSZTBS as 寝室状态标识,CWS as 床位数,BZ as 备注 from TBQS where qs_h>'"+lastqs_h+"' order by QS_H";
if (pagedq == page)
{
pagesm.Text = "当前为:第" + page + "页,共" + page + "页。";
}
else
{
pagesm.Text = "当前为:第" + (++pagedq) + "页,共" + page + "页。";
}
break;
default:
break;
}
myds = PublicCode.GetMySet(sqlselect);
if (myds.Tables[0].Rows.Count > 0)
{
dataGrid2.DataSource = myds.Tables[0];
}
}
相关下载:http://blog.bc-cn.net/user18/101647/archives/2006/2572.shtml