creazygirl

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

自定义数据分页--非常重要!(提高性能效率)
每次this.datashow();是提取全部数据,反而降低了效率。

正确的方法:
1,选中“允许分页”;“允许自定义分页”;页大小。
2,添加导航按钮,设置CommandName属性,previous,next
3,代码:

//记录每一页的开始索引
int startindex;

private void Page_Load(object sender, System.EventArgs e)
{
//自定义按钮事件
this.btnprevious.Click+=new System.EventHandler(this.NavigateToPage);
this.btnnext.Click+=new System.EventHandler(this.NavigateToPage);

//or OnCommand="NavigateToPage"

if(!IsPostBack)
{
startindex=0;

//得到数据源的记录数,并指派给DataGrid1

string constr="server=127.0.0.1;database=ltp;user id=sa;password=";
SqlConnection mycon=new SqlConnection(constr);
mycon.Open();

string sql="select 总数=count(*) from data";
SqlCommand com=new SqlCommand(sql,mycon);

SqlDataReader dr=com.ExecuteReader(CommandBehavior.SingleRow);
if(dr.Read())
this.DataGrid1.VirtualItemCount=(int)dr["总数"];
dr.Close();
mycon.Close();
this.bindGrid(startindex,"previous");

}
}

//自定义按钮事件
private void NavigateToPage(object sender,System.EventArgs e)
{
string pageinfo=((Button)sender).CommandName;
switch(pageinfo)
{
case "previous":
if(this.DataGrid1.CurrentPageIndex>0)
{
this.DataGrid1.CurrentPageIndex-=1;

}
break;

case "next":
if(this.DataGrid1.CurrentPageIndex<(this.DataGrid1.PageCount-1))
{
this.DataGrid1.CurrentPageIndex+=1;

}
break;

}

//得到开始的索引
startindex=this.DataGrid1.CurrentPageIndex*this.DataGrid1.PageSize;
//重新绑定
this.bindGrid(startindex,pageinfo);


}

//从数据源提取所需的数据记录--方法2(有int序号的表)
private void bindGrid2(int startindex,string pageinfo)
{
string constr="server=127.0.0.1;database=ltp;user id=sa;password=";
SqlConnection mycon=new SqlConnection(constr);
mycon.Open();

string sql="select top 5 * from data where 序号>="+startindex+" order by 序号";
SqlDataAdapter da=new SqlDataAdapter(sql,mycon);

DataSet ds=new DataSet();
da.Fill(ds,"data");
this.DataGrid1.DataSource=ds.Tables["data"];
this.DataGrid1.DataBind();

mycon.Close();


}


//从数据源提取所需的数据记录--方法1(按某字符串列排序的)
private void bindGrid(int startindex,string pageinfo)
{
string constr="server=127.0.0.1;database=ltp;user id=sa;password=";
SqlConnection mycon=new SqlConnection(constr);
mycon.Open();

SqlCommand com=new SqlCommand();

switch(pageinfo)
{
case "previous":
string sql="select top 5 * from data where 持股名称>=@id order by 持股名称";
com=new SqlCommand(sql,mycon);
if(startindex==0)
{
com.Parameters.Add("@id",SqlDbType.NVarChar,10).Value="";
}
else
{
com.Parameters.Add("@id",SqlDbType.NVarChar,10).Value=ViewState[(this.DataGrid1.CurrentPageIndex+1).ToString()];
}
break;
case "next":
string sql2="select top 5 * from data where 持股名称>@id order by 持股名称";
com=new SqlCommand(sql2,mycon);

//把最后一行的列值赋给下一页开始
com.Parameters.Add("@id",SqlDbType.NVarChar,10).Value=this.DataGrid1.Items[4].Cells[1].Text;
break;
}

SqlDataReader dr=com.ExecuteReader();
this.DataGrid1.DataSource=dr;
this.DataGrid1.DataBind();
dr.Close();
mycon.Close();

//重新得到当前开始第一行的列值
ViewState[(this.DataGrid1.CurrentPageIndex+1).ToString()]=this.DataGrid1.Items[0].Cells[1].Text;


}

posted on 2006-06-09 17:53  久久  阅读(231)  评论(0编辑  收藏  举报