DataGrid分页通用
void initpage()
{
string topsql = string.Format(@"select count(a.ProductCode) from Production as a,MyCompanyWeb as b,Pictures as c
where a.UserSID=b.UserSID and a.ProductCode=c.ProductCode and OneFunSID={0} and State=1",onefunid);
object o=this.Db.CreateStatement(topsql).GetScalar();
int rowCount=0;
if(o==null)
{
CurrentPageIndex=0;
return;
}
rowCount=Convert.ToInt32(o.ToString ());
if(rowCount%PageSize==0)
{
PageCount=rowCount/PageSize;
}
else
{
PageCount=rowCount/PageSize+1;
}
lblCount.Text=PageCount.ToString();
if(PageCount>0)
{
CurrentPageIndex=1;
GridBind();
}
else
{
DataList1.DataSource = new DataTable ();
DataList1.DataBind ();
}
SetLinkLutton();
}
void GridBind()
{
string sqlstr=string.Empty;
if(CurrentPageIndex==1)
{
sqlstr=String.Format (@"select top {0} a.ProductSID,a.ProductCode,ProductName,a.CreateTime,Quality,KeyWords,Amount,b.Company,c.ImageUrl1
from Production as a,MyCompanyWeb as b,Pictures as c
where a.UserSID=b.UserSID and a.ProductCode=c.ProductCode and OneFunSID={1} and State=1 order by a.BallotNew desc,a.CreateTime desc",
this.PageSize,onefunid);
}
else
{
int cindex = (CurrentPageIndex-1)*PageSize;
sqlstr=String.Format (@"select top {0} a.ProductSID,a.ProductCode,ProductName,a.CreateTime,Quality,KeyWords,Amount,b.Company,c.ImageUrl1
from Production as a,MyCompanyWeb as b,Pictures as c
where a.ProductSID not in (select top {1} a.ProductSID from Production as a,MyCompanyWeb as b,Pictures as c
where a.UserSID=b.UserSID and a.ProductCode=c.ProductCode and a.OneFunSID={2} and State=1 order by a.BallotNew desc,a.CreateTime desc)
and a.UserSID=b.UserSID and a.ProductCode=c.ProductCode and OneFunSID={2} and State=1 order by a.BallotNew desc,a.CreateTime desc",
this.PageSize.ToString(),cindex.ToString(),onefunid);
}
DataTable tb=Db.CreateStatement (sqlstr).GetDataTable ();
if(tb!=null && tb.Rows.Count>0)
{
DataList1.DataSource =tb;
DataList1.DataBind ();
}
else
{
DataList1.DataSource = new DataTable ();
DataList1.DataBind ();
}
SetLinkLutton();
lblCur.Text =CurrentPageIndex.ToString();
}
public int CurrentPageIndex
{
get
{
object o= ViewState["CurrentPageIndex"];
return o==null?0:Convert.ToInt32(o);
}
set
{
ViewState["CurrentPageIndex"]=value;
}
}
public int PageCount
{
get
{
object o= ViewState["PageCount"];
return o==null?0:Convert.ToInt32(o);
}
set
{
ViewState["PageCount"]=value;
}
}
public string sqlWhere
{
get
{
return (string)ViewState["SqlWhere"];
}
set
{
ViewState["SqlWhere"]=value;
}
}
void SetLinkLutton()
{
if(CurrentPageIndex==0)
{
lbtFirst.Enabled =false;
lbtPre.Enabled =false;
lbtNext.Enabled =false;
lbtLast.Enabled =false;
}
else if(CurrentPageIndex==1)
{
lbtFirst.Enabled =false;
lbtPre.Enabled =false;
lbtNext.Enabled =true;
lbtLast.Enabled =true;
if(CurrentPageIndex==PageCount)
{
lbtNext.Enabled =false;
lbtLast.Enabled =false;
}
}
else if(CurrentPageIndex==PageCount)
{
lbtFirst.Enabled =true;
lbtPre.Enabled =true;
lbtNext.Enabled =false;
lbtLast.Enabled =false;
}
else
{
lbtFirst.Enabled =true;
lbtPre.Enabled =true;
lbtNext.Enabled =true;
lbtLast.Enabled =true;
}
}
private void lbtLast_Click(object sender, System.EventArgs e)
{
this.CurrentPageIndex=PageCount;
GridBind();
}
private void lbtNext_Click(object sender, System.EventArgs e)
{
if(CurrentPageIndex<PageCount)
{
this.CurrentPageIndex=CurrentPageIndex+1;
GridBind();
}
}
private void lbtPre_Click(object sender, System.EventArgs e)
{
if(CurrentPageIndex>1)
{
this.CurrentPageIndex=CurrentPageIndex-1;
GridBind();
}
}
private void lbtFirst_Click(object sender, System.EventArgs e)
{
this.CurrentPageIndex=1;
GridBind();
}
void initpage()
{
string topsql = string.Format(@"select count(a.ProductCode) from Production as a,MyCompanyWeb as b,Pictures as c
where a.UserSID=b.UserSID and a.ProductCode=c.ProductCode and OneFunSID={0} and State=1",onefunid);
object o=this.Db.CreateStatement(topsql).GetScalar();
int rowCount=0;
if(o==null)
{
CurrentPageIndex=0;
return;
}
rowCount=Convert.ToInt32(o.ToString ());
if(rowCount%PageSize==0)
{
PageCount=rowCount/PageSize;
}
else
{
PageCount=rowCount/PageSize+1;
}
lblCount.Text=PageCount.ToString();
if(PageCount>0)
{
CurrentPageIndex=1;
GridBind();
}
else
{
DataList1.DataSource = new DataTable ();
DataList1.DataBind ();
}
SetLinkLutton();
}
void GridBind()
{
string sqlstr=string.Empty;
if(CurrentPageIndex==1)
{
sqlstr=String.Format (@"select top {0} a.ProductSID,a.ProductCode,ProductName,a.CreateTime,Quality,KeyWords,Amount,b.Company,c.ImageUrl1
from Production as a,MyCompanyWeb as b,Pictures as c
where a.UserSID=b.UserSID and a.ProductCode=c.ProductCode and OneFunSID={1} and State=1 order by a.BallotNew desc,a.CreateTime desc",
this.PageSize,onefunid);
}
else
{
int cindex = (CurrentPageIndex-1)*PageSize;
sqlstr=String.Format (@"select top {0} a.ProductSID,a.ProductCode,ProductName,a.CreateTime,Quality,KeyWords,Amount,b.Company,c.ImageUrl1
from Production as a,MyCompanyWeb as b,Pictures as c
where a.ProductSID not in (select top {1} a.ProductSID from Production as a,MyCompanyWeb as b,Pictures as c
where a.UserSID=b.UserSID and a.ProductCode=c.ProductCode and a.OneFunSID={2} and State=1 order by a.BallotNew desc,a.CreateTime desc)
and a.UserSID=b.UserSID and a.ProductCode=c.ProductCode and OneFunSID={2} and State=1 order by a.BallotNew desc,a.CreateTime desc",
this.PageSize.ToString(),cindex.ToString(),onefunid);
}
DataTable tb=Db.CreateStatement (sqlstr).GetDataTable ();
if(tb!=null && tb.Rows.Count>0)
{
DataList1.DataSource =tb;
DataList1.DataBind ();
}
else
{
DataList1.DataSource = new DataTable ();
DataList1.DataBind ();
}
SetLinkLutton();
lblCur.Text =CurrentPageIndex.ToString();
}
public int CurrentPageIndex
{
get
{
object o= ViewState["CurrentPageIndex"];
return o==null?0:Convert.ToInt32(o);
}
set
{
ViewState["CurrentPageIndex"]=value;
}
}
public int PageCount
{
get
{
object o= ViewState["PageCount"];
return o==null?0:Convert.ToInt32(o);
}
set
{
ViewState["PageCount"]=value;
}
}
public string sqlWhere
{
get
{
return (string)ViewState["SqlWhere"];
}
set
{
ViewState["SqlWhere"]=value;
}
}
void SetLinkLutton()
{
if(CurrentPageIndex==0)
{
lbtFirst.Enabled =false;
lbtPre.Enabled =false;
lbtNext.Enabled =false;
lbtLast.Enabled =false;
}
else if(CurrentPageIndex==1)
{
lbtFirst.Enabled =false;
lbtPre.Enabled =false;
lbtNext.Enabled =true;
lbtLast.Enabled =true;
if(CurrentPageIndex==PageCount)
{
lbtNext.Enabled =false;
lbtLast.Enabled =false;
}
}
else if(CurrentPageIndex==PageCount)
{
lbtFirst.Enabled =true;
lbtPre.Enabled =true;
lbtNext.Enabled =false;
lbtLast.Enabled =false;
}
else
{
lbtFirst.Enabled =true;
lbtPre.Enabled =true;
lbtNext.Enabled =true;
lbtLast.Enabled =true;
}
}
private void lbtLast_Click(object sender, System.EventArgs e)
{
this.CurrentPageIndex=PageCount;
GridBind();
}
private void lbtNext_Click(object sender, System.EventArgs e)
{
if(CurrentPageIndex<PageCount)
{
this.CurrentPageIndex=CurrentPageIndex+1;
GridBind();
}
}
private void lbtPre_Click(object sender, System.EventArgs e)
{
if(CurrentPageIndex>1)
{
this.CurrentPageIndex=CurrentPageIndex-1;
GridBind();
}
}
private void lbtFirst_Click(object sender, System.EventArgs e)
{
this.CurrentPageIndex=1;
GridBind();
}