人人做人人澡人人爽欧美,国产主播一区二区,久久久精品五月天,羞羞视频在线观看免费

當(dāng)前位置:蘿卜系統(tǒng)下載站 > 技術(shù)開(kāi)發(fā)教程 > 詳細(xì)頁(yè)面

DataGrid分頁(yè)運(yùn)用小結(jié)

DataGrid分頁(yè)運(yùn)用小結(jié)

更新時(shí)間:2022-07-25 文章作者:未知 信息來(lái)源:網(wǎng)絡(luò) 閱讀次數(shù):


默認(rèn)分頁(yè)模式:
選中“允許分頁(yè)”;頁(yè)大小;頁(yè)導(dǎo)航設(shè)置,可以是上下方式,也可以用頁(yè)碼方式
格式里可以設(shè)置“頁(yè)導(dǎo)航”按鈕的對(duì)起方式;

private void datashow()//綁定數(shù)據(jù)
{
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();

}

響應(yīng)事件 PageIndexChanged()

this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
datashow();





自定義導(dǎo)航控件的默認(rèn)分頁(yè)模式
當(dāng)前頁(yè):this.Label1.Text=(this.DataGrid1.CurrentPageIndex+1).ToString();
因?yàn)镃urrentPageIndex從0開(kāi)始的,所以要+1

總頁(yè)數(shù):this.Label2.Text=this.DataGrid1.PageCount.ToString();

//第一頁(yè)
this.DataGrid1.CurrentPageIndex=0;
//上一頁(yè)
if(this.DataGrid1.CurrentPageIndex>0)
{
this.DataGrid1.CurrentPageIndex-=1;
this.datashow();
}
//下一頁(yè)
if(this.DataGrid1.CurrentPageIndex<(this.DataGrid1.PageCount-1))
{
this.DataGrid1.CurrentPageIndex+=1;
this.datashow();
}
//最后一頁(yè)
this.DataGrid1.CurrentPageIndex=this.DataGrid1.PageCount-1


最后再 datashow();



自定義數(shù)據(jù)分頁(yè)--非常重要!(提高性能效率)
每次this.datashow();是提取全部數(shù)據(jù),反而降低了效率。

正確的方法:
1,選中“允許分頁(yè)”;“允許自定義分頁(yè)”;頁(yè)大小。
2,添加導(dǎo)航按鈕,設(shè)置CommandName屬性,previous,next
3,代碼:

//記錄每一頁(yè)的開(kāi)始索引
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;

//得到數(shù)據(jù)源的記錄數(shù),并指派給DataGrid1

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

string sql="select 總數(shù)=count(*) from data";
SqlCommand com=new SqlCommand(sql,mycon);

SqlDataReader dr=com.ExecuteReader(CommandBehavior.SingleRow);
if(dr.Read())
this.DataGrid1.VirtualItemCount=(int)dr["總數(shù)"];
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;

}

//得到開(kāi)始的索引
startindex=this.DataGrid1.CurrentPageIndex*this.DataGrid1.PageSize;
//重新綁定
this.bindGrid(startindex,pageinfo);


}

//從數(shù)據(jù)源提取所需的數(shù)據(jù)記錄--方法2(有int序號(hào)的表)
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 序號(hào)>="+startindex+" order by 序號(hào)";
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();


}


//從數(shù)據(jù)源提取所需的數(shù)據(jù)記錄--方法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
{
//把開(kāi)始
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);

//把最后一行的列值賦給下一頁(yè)開(kāi)始
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();

//重新得到當(dāng)前開(kāi)始第一行的列值
ViewState[(this.DataGrid1.CurrentPageIndex+1).ToString()]=this.DataGrid1.Items[0].Cells[1].Text;


}

溫馨提示:喜歡本站的話,請(qǐng)收藏一下本站!

本類教程下載

系統(tǒng)下載排行

網(wǎng)站地圖xml | 網(wǎng)站地圖html
主站蜘蛛池模板: 久治县| 甘谷县| 河东区| 高平市| 临清市| 乐东| 无锡市| 平阴县| 泰和县| 交城县| 昌平区| 寿宁县| 巴林右旗| 恩平市| 芒康县| 中牟县| 新野县| 德阳市| 伊通| 二连浩特市| 米脂县| 景谷| 陵川县| 桑植县| 清镇市| 兴安县| 威宁| 怀化市| 安乡县| 福泉市| 临桂县| 新宾| 廊坊市| 金寨县| 兴安盟| 如皋市| 广元市| 长白| 石林| 壤塘县| 平谷区|