六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

DataGrid分頁使用小結

[摘要]默認分頁模式: 選中“允許分頁”;頁大小;頁導航設置,可以是上下方式,也可以用頁碼方式 格式里可以設置“頁導航”按鈕的對起方式; private void datashow()//綁定數據 string sql="server=127.0.0.1;database=ltp;user i...

默認分頁模式:
選中“允許分頁”;頁大小;頁導航設置,可以是上下方式,也可以用頁碼方式
格式里可以設置“頁導航”按鈕的對起方式;

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;


}



主站蜘蛛池模板: 色综合五月 | 日本不卡高清视频 | 亚洲第一区精品观看 | 午夜免费啪在线观看视频网站 | 青青久在线视频 | 色综合综合 | 亚洲国产欧美另类 | 亚洲三级欧美 | 日韩三级免费观看 | 日日摸夜夜添欧美一区 | 最新韩国伦理片大全手机在线播放 | 亚洲天堂网视频 | 速度与激情9全集免费观看 速度与激情9免费完整版高清 | 午夜视频在线观看网站 | 午夜爽视频 | 欧美性xxxxx极品老少 | 亚洲欧美在线视频 | 日本久久综合网 | 午夜在线观看免费观看大全 | 又粗又硬又爽的三级视频在线观看 | 日韩一区二紧身裤 | 日韩免费高清一级毛片在线 | 亚洲欧洲一区二区三区在线观看 | 天干天干天啪啪夜爽爽色 | 天天摸天天澡天天碰天天弄 | 亚洲福利网址 | 亚洲国产一区二区在线 | 青娱乐91视频 | 亚洲最新在线 | 日本免费看片在线播放 | 青青精品 | 天天干夜夜曰 | 日韩一区二区中文字幕 | 亚洲视频国产视频 | 青娱乐97 | 亚洲福利在线观看 | 天天色天天色天天色 | 午夜毛片免费看 | 青草青草伊人精品视频 | 日韩成片 | 息与子外出中文字幕 |