惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
C
Check Point Blog
Vercel News
Vercel News
Y
Y Combinator Blog
G
Google Developers Blog
P
Proofpoint News Feed
WordPress大学
WordPress大学
MongoDB | Blog
MongoDB | Blog
博客园 - 司徒正美
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
N
Netflix TechBlog - Medium
L
LangChain Blog
V
V2EX
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
大猫的无限游戏
大猫的无限游戏
D
DataBreaches.Net
博客园_首页
B
Blog RSS Feed
The Cloudflare Blog
MyScale Blog
MyScale Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Security Blog
Microsoft Security Blog

博客园 - 久久

终于找到回归CNBLOG的理由 六度空间理论(小世界理论)否定了结构洞的存在 人生真是变化无常 我的儿时理想—让世界没有乞丐 挥之不去的技术情节 告别9月 首次尝试电话营销的味道 在textbox中加入html字符 用access数据库时的相对路径设置 将图片直接传到数据库的方法 关于treeview的一些知识点 好久没有上来更新资料了,有点对不起自己了 初试牛刀的收获-freetextbox的应用 初试牛刀的收获-实现Web文件的上传 初试牛刀的收获-浅尝session 初试牛刀的收获_运算符比较 .net学习笔记datalist编辑模板的应用 .net学习笔记datalist产品购买实例20060602 .net学习笔记datagrid默认、自定义分页20060531
初试牛刀的感受_datagrid自定义分页
久久 · 2006-06-09 · via 博客园 - 久久

自定义数据分页--非常重要!(提高性能效率)
每次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;


}