1 在绑定DataGrid方法设置允许分页;
2 在绑定DataGrid方法里初始化;
3 分页控制按钮属性
4 分页返回总页数,并初始化按钮控件;
5 linkBtn控件事件;
=======================================
1 设置允许分页;
this.dgbend.PageSize=10;
this.dgbend.AllowPaging=true;
2 在绑定DataGrid页面里初始化;
#region //初始化页面分页
if(ds.Tables.Count>0)
{
if(ds.Tables[0].Rows.Count>0)
{
if(this.dgbend.PageCount>0)
{
#region
int indexnum = this.dgbend.CurrentPageIndex;
int pagenum = this.dgbend.PageCount;
if(indexnum==0)
{
this.labnum.Text="当前是第"+1+"页," +"总共"+pagenum+"页";
this.btnnext.Enabled =true;
this.btnlast.Enabled =true;
this.btnfirst.Enabled=false;
this.btnprev.Enabled =false;
}
if(indexnum > 0)
{
if(indexnum < pagenum)
{
indexnum=indexnum+1;
this.labnum.Text="当前是第"+indexnum+"页," +"总共"+pagenum+"页";
}
if(indexnum == pagenum)
{
indexnum=indexnum;
this.labnum.Text="当前是第"+indexnum+"页," +"总共"+pagenum+"页";
}
}
if(this.dgbend.PageCount==1)
{
this.labnum.Text="当前是第"+1+"页," +"总共"+1+"页";
this.btnnext.Enabled =false;
this.btnlast.Enabled =false;
this.btnfirst.Enabled=false;
this.btnprev.Enabled =false;
}
#endregion
}
}
if(ds.Tables[0].Rows.Count==0)
{
this.labnum.Visible=false;
this.labnum.Text="没有记录!";
}
}
#endregion
3 //分页控制按钮属性
private void btnEnabled(int indexpage)
{
int pageNum=NavigatBtn();
if(pageNum ==0)
{
this.labnum.Text="没有记录!";
this.btnfirst.Enabled =false;
this.btnprev.Enabled =false;
this.btnnext.Enabled =false;
this.btnlast.Enabled =false;
}
if(pageNum-1 == 1)
{
this.btnfirst.Enabled =true;
this.btnprev.Enabled =false;
this.btnnext.Enabled =false;
this.btnlast.Enabled =false;
}
if(pageNum > 1)
{
if(pageNum-1 == indexpage)
{
this.btnfirst.Enabled =true;
this.btnprev.Enabled =true;
this.btnnext.Enabled =false;
this.btnlast.Enabled =false;
}
if(indexpage < pageNum-1)
{
this.btnfirst.Enabled =true;
this.btnprev.Enabled =true;
this.btnnext.Enabled =true;
this.btnlast.Enabled =true;
}
if(indexpage ==0)
{
this.btnfirst.Enabled =false;
this.btnprev.Enabled =false;
this.btnnext.Enabled =true;
this.btnlast.Enabled =true;
}
}
}
4 //分页返回总页数,并初始化按钮控件
private int NavigatBtn()
{
int pageNum=0;
if(this.dgbend.PageCount>0)
{
pageNum =this.dgbend.PageCount;
}
if(pageNum == 1)
{
this.btnfirst.Enabled =true;
this.btnprev.Enabled =false;
this.btnnext.Enabled =false;
this.btnlast.Enabled =false;
}
if(pageNum > 1)
{
this.btnfirst.Enabled =true;
this.btnprev.Enabled =true;
this.btnnext.Enabled =true;
this.btnlast.Enabled =true;
}
return pageNum;
}
5 linkBtn控件事件;
private void btnfirst_Click(object sender, System.EventArgs e)
{
this.dgbend.CurrentPageIndex=0;
BindGrid();
btnEnabled(0);
}
private void btnprev_Click(object sender, System.EventArgs e)
{
this.dgbend.CurrentPageIndex=System.Math.Max(this.dgbend.CurrentPageIndex -1,0);
BindGrid();
btnEnabled(this.dgbend.CurrentPageIndex);
}
private void btnnext_Click(object sender, System.EventArgs e)
{
this.dgbend.CurrentPageIndex=System.Math.Min(this.dgbend.PageCount -1,this.dgbend.CurrentPageIndex +1);
BindGrid();
btnEnabled(this.dgbend.CurrentPageIndex);
}
private void btnlast_Click(object sender, System.EventArgs e)
{
this.dgbend.CurrentPageIndex=this.dgbend.PageCount -1;
BindGrid();
btnEnabled(this.dgbend.CurrentPageIndex);
}
------------------------------linkbtn---------
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!this.Page.IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
SqlConnection cnn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
//创建数据适配器对象
SqlDataAdapter da = new SqlDataAdapter("select ProductID,ProductName from Products order by ProductID",cnn);
//创建DataSet对象
DataSet ds = new DataSet();
try
{
//填充数据集
da.Fill(ds, "testTable");
//进行数据绑定
this.dgbend.PageSize=10;
this.dgbend.AllowPaging=true;
dgbend.DataSource = ds;
dgbend.DataBind();
#region //初始化页面分页
if(ds.Tables.Count>0)
{
if(ds.Tables[0].Rows.Count>0)
{
if(this.dgbend.PageCount>0)
{
#region
int indexnum = this.dgbend.CurrentPageIndex;
int pagenum = this.dgbend.PageCount;
if(indexnum==0)
{
this.labnum.Text="当前是第"+1+"页," +"总共"+pagenum+"页";
this.btnnext.Enabled =true;
this.btnlast.Enabled =true;
this.btnfirst.Enabled=false;
this.btnprev.Enabled =false;
}
if(indexnum > 0)
{
if(indexnum < pagenum)
{
indexnum=indexnum+1;
this.labnum.Text="当前是第"+indexnum+"页," +"总共"+pagenum+"页";
}
if(indexnum == pagenum)
{
indexnum=indexnum;
this.labnum.Text="当前是第"+indexnum+"页," +"总共"+pagenum+"页";
}
}
if(this.dgbend.PageCount==1)
{
this.labnum.Text="当前是第"+1+"页," +"总共"+1+"页";
this.btnnext.Enabled =false;
this.btnlast.Enabled =false;
this.btnfirst.Enabled=false;
this.btnprev.Enabled =false;
}
#endregion
}
}
if(ds.Tables[0].Rows.Count==0)
{
this.labnum.Visible=false;
this.labnum.Text="没有记录!";
}
}
#endregion
}
catch(Exception error)
{
Response.Write(error.ToString());
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnfirst.Click += new System.EventHandler(this.btnfirst_Click);
this.btnprev.Click += new System.EventHandler(this.btnprev_Click);
this.btnnext.Click += new System.EventHandler(this.btnnext_Click);
this.btnlast.Click += new System.EventHandler(this.btnlast_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnfirst_Click(object sender, System.EventArgs e)
{
this.dgbend.CurrentPageIndex=0;
BindGrid();
btnEnabled(0);
}
private void btnprev_Click(object sender, System.EventArgs e)
{
this.dgbend.CurrentPageIndex=System.Math.Max(this.dgbend.CurrentPageIndex -1,0);
BindGrid();
btnEnabled(this.dgbend.CurrentPageIndex);
}
private void btnnext_Click(object sender, System.EventArgs e)
{
this.dgbend.CurrentPageIndex=System.Math.Min(this.dgbend.PageCount -1,this.dgbend.CurrentPageIndex +1);
BindGrid();
btnEnabled(this.dgbend.CurrentPageIndex);
}
private void btnlast_Click(object sender, System.EventArgs e)
{
this.dgbend.CurrentPageIndex=this.dgbend.PageCount -1;
BindGrid();
btnEnabled(this.dgbend.CurrentPageIndex);
}
//分页控制按钮属性
private void btnEnabled(int indexpage)
{
int pageNum=NavigatBtn();
if(pageNum ==0)
{
this.labnum.Text="没有记录!";
this.btnfirst.Enabled =false;
this.btnprev.Enabled =false;
this.btnnext.Enabled =false;
this.btnlast.Enabled =false;
}
if(pageNum-1 == 1)
{
this.btnfirst.Enabled =true;
this.btnprev.Enabled =false;
this.btnnext.Enabled =false;
this.btnlast.Enabled =false;
}
if(pageNum > 1)
{
if(pageNum-1 == indexpage)
{
this.btnfirst.Enabled =true;
this.btnprev.Enabled =true;
this.btnnext.Enabled =false;
this.btnlast.Enabled =false;
}
if(indexpage < pageNum-1)
{
this.btnfirst.Enabled =true;
this.btnprev.Enabled =true;
this.btnnext.Enabled =true;
this.btnlast.Enabled =true;
}
if(indexpage ==0)
{
this.btnfirst.Enabled =false;
this.btnprev.Enabled =false;
this.btnnext.Enabled =true;
this.btnlast.Enabled =true;
}
}
}
//分页返回总页数,并初始化按钮控件
private int NavigatBtn()
{
int pageNum=0;
if(this.dgbend.PageCount>0)
{
pageNum =this.dgbend.PageCount;
}
if(pageNum == 1)
{
this.btnfirst.Enabled =true;
this.btnprev.Enabled =false;
this.btnnext.Enabled =false;
this.btnlast.Enabled =false;
}
if(pageNum > 1)
{
this.btnfirst.Enabled =true;
this.btnprev.Enabled =true;
this.btnnext.Enabled =true;
this.btnlast.Enabled =true;
}
return pageNum;
}
}
}
=================================优化================
//分页控制按钮属性
private void btnEnabled(DataGrid dg,int indexpage,LinkButton btnfirst,LinkButton btnprev,LinkButton btnnext,LinkButton btnlast)
{
int pageNum=NavigatBtn(dg,btnfirst,btnprev,btnnext,btnlast);
if(pageNum ==0)
{
//this.labnum.Text="没有记录!";
btnfirst.Enabled =false;
btnprev.Enabled =false;
btnnext.Enabled =false;
btnlast.Enabled =false;
}
if(pageNum-1 == 1)
{
btnfirst.Enabled =true;
btnprev.Enabled =false;
btnnext.Enabled =false;
btnlast.Enabled =false;
}
if(pageNum > 1)
{
if(pageNum-1 == indexpage)
{
btnfirst.Enabled =true;
btnprev.Enabled =true;
btnnext.Enabled =false;
btnlast.Enabled =false;
}
if(indexpage < pageNum-1)
{
btnfirst.Enabled =true;
btnprev.Enabled =true;
btnnext.Enabled =true;
btnlast.Enabled =true;
}
if(indexpage ==0)
{
btnfirst.Enabled =false;
btnprev.Enabled =false;
btnnext.Enabled =true;
btnlast.Enabled =true;
}
}
}
//分页返回总页数,并初始化按钮控件
private static int NavigatBtn(DataGrid dg,LinkButton btnfirst,LinkButton btnprev,LinkButton btnnext,LinkButton btnlast)
{
int pageNum=0;
if(dg.PageCount>0)
{
pageNum =dg.PageCount;
}
if(pageNum == 1)
{
btnfirst.Enabled =true;
btnprev.Enabled =false;
btnnext.Enabled =false;
btnlast.Enabled =false;
}
if(pageNum > 1)
{
btnfirst.Enabled =true;
btnprev.Enabled =true;
btnnext.Enabled =true;
btnlast.Enabled =true;
}
return pageNum;
}
public static void fenye(DataGrid dg,LinkButton btnfirst,LinkButton btnprev,LinkButton btnnext,LinkButton btnlast,Label labnum)
{
#region
int indexnum = dg.CurrentPageIndex;
int pagenum = dg.PageCount;
if(indexnum==0)
{
labnum.Text="当前是第"+1+"页," +"总共"+pagenum+"页";
btnnext.Enabled =true;
btnlast.Enabled =true;
btnfirst.Enabled=false;
btnprev.Enabled =false;
}
if(indexnum > 0)
{
if(indexnum < pagenum)
{
indexnum=indexnum+1;
labnum.Text="当前是第"+indexnum+"页," +"总共"+pagenum+"页";
}
if(indexnum == pagenum)
{
indexnum=indexnum;
labnum.Text="当前是第"+indexnum+"页," +"总共"+pagenum+"页";
}
}
if(dg.PageCount==1)
{
labnum.Text="当前是第"+1+"页," +"总共"+1+"页";
btnnext.Enabled =false;
btnlast.Enabled =false;
btnfirst.Enabled=false;
btnprev.Enabled =false;
}
#endregion
}
}
}
===================csj.08.06.30======================
/// <summary>
///分页操作 csj_080630
/// </summary>
/// <param name="lable">显示共多少页,当前是第几页 lable</param>
/// <param name="lkbtn">第一页,上一页,下一面,最后页 linkbtn</param>
/// <param name="dggrid">要分页的datagrid</param>
/// <param name="pagesize">设置每页条数</param>
public static void DataGridPageNaivegate(System.Web.UI.WebControls.Label lable,System.Web.UI.WebControls.LinkButton[] lkbtn,System.Web.UI.WebControls.DataGrid dggrid,string pagesize)
{
int pageNum=0;
dggrid.PageSize=int.Parse(pagesize.Trim().ToString());
if(dggrid.PageCount>=0)
{
pageNum = dggrid.PageCount;
}
if(pageNum==0)
{
lable.Visible = false;
lable.Text = "没有记录!";
lkbtn[0].Enabled = false;
lkbtn[1].Enabled = false;
lkbtn[2].Enabled = false;
lkbtn[3].Enabled = false;
}
if(pageNum> 0)
{
int indexnum = dggrid.CurrentPageIndex; //获取当前页索引
int pagenum = dggrid.PageCount; //获取页总数
if(indexnum==0)
{
lable.Text="当前是第"+1+"页," +"总共"+pagenum+"页";
lkbtn[0].Enabled = false;
lkbtn[1].Enabled = false;
lkbtn[2].Enabled = true;
lkbtn[3].Enabled = true;
}
if(indexnum > 0)
{
if(indexnum < pagenum)
{
indexnum=indexnum+1;
lable.Text = "当前是第"+indexnum+"页," +"总共"+pagenum+"页";
lkbtn[0].Enabled = true;
lkbtn[1].Enabled = true;
lkbtn[2].Enabled = true;
lkbtn[3].Enabled = true;
}
if(indexnum == pagenum)
{
indexnum=indexnum;
lable.Text = "当前是第"+indexnum+"页," +"总共"+pagenum+"页";
lkbtn[0].Enabled = true;
lkbtn[1].Enabled = true;
lkbtn[2].Enabled = false;
lkbtn[3].Enabled = false;
}
}
if(dggrid.PageCount==1)
{
lable.Text="当前是第"+1+"页," +"总共"+1+"页";
lkbtn[0].Enabled = false;
lkbtn[1].Enabled = false;
lkbtn[2].Enabled = false;
lkbtn[3].Enabled = false;
}
MaterielOperate.QueryMateriel(ref dggrid,"");
}
}
=================================================