分享
 
 
 

DataGrid分页使用小结

王朝asp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

默认分页模式:

选中“允许分页”;页大小;页导航设置,可以是上下方式,也可以用页码方式

格式里可以设置“页导航”按钮的对起方式;

private void datashow()//绑定数据

{

string sql="server=127.0.0.1;database=ltp;user id=sa;password=";

SqlConnection mycon=new SqlConnection(sql);

string selsql="select * from data";

SqlDataAdapter da=new SqlDataAdapter(selsql,mycon);

DataSet ds=new DataSet();

da.Fill(ds,"data");

this.DataGrid1.DataSource=ds.Tables["data"];

this.DataGrid1.DataBind();

}

响应事件 PageIndexChanged()

this.DataGrid1.CurrentPageIndex=e.NewPageIndex;

datashow();

自定义导航控件的默认分页模式

当前页:this.Label1.Text=(this.DataGrid1.CurrentPageIndex+1).ToString();

因为CurrentPageIndex从0开始的,所以要+1

总页数:this.Label2.Text=this.DataGrid1.PageCount.ToString();

//第一页

this.DataGrid1.CurrentPageIndex=0;

//上一页

if(this.DataGrid1.CurrentPageIndex>0)

{

this.DataGrid1.CurrentPageIndex-=1;

this.datashow();

}

//下一页

if(this.DataGrid1.CurrentPageIndex<(this.DataGrid1.PageCount-1))

{

this.DataGrid1.CurrentPageIndex+=1;

this.datashow();

}

//最后一页

this.DataGrid1.CurrentPageIndex=this.DataGrid1.PageCount-1

最后再 datashow();

自定义数据分页--非常重要!(提高性能效率)

每次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);

// com=new SqlCommand("select top 5 * from data where 持股名称>=@id order by 持股名称",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()];

// com.Parameters.Add("@id",SqlDbType.NVarChar,10).Value=this.DataGrid1.Items[0].Cells[1].Text;

}

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;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有