国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

如何在DataGrid控件中實現自定義分頁

2024-07-10 13:14:02
字體:
來源:轉載
供稿:網友
如何在DataGrid控件中實現自定義分頁 
在一般情況下,DataGrid控件每次實現翻頁操作時,都會將數據源中的數據重新調用一次,當數據中 
數據很多時,這樣做就會很浪費系統資源和降低程序的執行效率.這時候我們一般通過自定義分頁來解 
決這個問題. 
DataGrid控件的AllowCustomPaging屬性用來獲取或設置DataGrid控件是否允許自定義分 
頁;VirtualItemCoun屬性用來獲取或設置在使用自定義分頁時DataGrid中實際的項數.要實現自定義分 
頁,必須將AllowPaging與AllowCustomPaging屬性都設置為"True". 
在DataGrid中要實現自定義分頁的關鍵是,使該控件僅僅調用當前顯示所需要的數據源數據,在下 
面的例子中通過CurrentPageIndex和PageSize屬性的值,在數據綁定時只取當前頁需要的數據. 
(1)頁面代碼: 
復制代碼代碼如下:

<%@ Page language="c#" Codebehind="Main.aspx.cs" AutoEventWireup="false" 
Inherits="SissonDemo.Main" %> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > 
<HTML> 
<HEAD> 
<title>Main</title> 
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> 
<meta name="CODE_LANGUAGE" Content="C#"> 
<meta name="vs_defaultClientScript" content="JavaScript"> 
<meta name="vs_targetSchema" 
content="http://schemas.microsoft.com/intellisense/ie5"> 
</HEAD> 
<body MS_POSITIONING="GridLayout"> 
<form id="Form1" method="post" runat="server"> 
<FONT face="宋體"> 
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 8px; POSITION: 
absolute; TOP: 24px" runat="server" 
Width="792px" Height="96px" AllowCustomPaging="True" 
AllowPaging="True" PageSize="5"> 
<PagerStyle Mode="NumericPages"></PagerStyle> 
</asp:DataGrid></FONT> 
</form> 
</body> 
</HTML>

(2)后臺代碼: 
復制代碼代碼如下:

using System; 
using System.Collections; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Web; 
using System.Web.SessionState; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.HtmlControls; 
using System.Data.SqlClient; 

namespace SissonDemo 

/**//// <summary> 
/// Main 的摘要說明。 
/// </summary> 
public class Main : System.Web.UI.Page 

protected System.Web.UI.WebControls.DataGrid DataGrid1; 
int startIndex = 0;//用來保存當前頁數據項的起始索引 
private void Page_Load(object sender, System.EventArgs e) 

if(!IsPostBack) 

Bind();//初始化時進行數據綁定 



void Bind()//綁定數據方法 
{ //定義數據庫連接對象 
SqlConnection cn=new SqlConnection("server=.;database=pubs;uid=sa;pwd="); 
//創建數據適配對象 
SqlDataAdapter da=new SqlDataAdapter("select title_id ,title ,type, pub_id 
,price,pubdate from titles",cn); 
//創建DataSet對象 
DataSet ds=new DataSet(); 
try 
{ //從指定的索引開始取PageSize條記錄. 
da.Fill(ds,startIndex,DataGrid1.PageSize,"CurDataTable"); 
da.Fill(ds,"AllDataTable");//填充數據集合 
//設置DataGrid控件實際要顯示的項數 
DataGrid1.VirtualItemCount=ds.Tables["AllDataTable"].Rows.Count; 
//進行數據綁定 
DataGrid1.DataSource=ds.Tables["CurDataTable"]; 
DataGrid1.DataBind(); 


catch 

Page.RegisterClientScriptBlock("","<script>alert('數據顯示錯 
誤');</script>"); 




Web 窗體設計器生成的代碼#region Web 窗體設計器生成的代碼 
override protected void OnInit(EventArgs e) 

// 
// CODEGEN: 該調用是 ASP.NET Web 窗體設計器所必需的。 
// 
InitializeComponent(); 
base.OnInit(e); 


/**//// <summary> 
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改 
/// 此方法的內容。 
/// </summary> 
private void InitializeComponent() 

this.DataGrid1.PageIndexChanged += new 
System.Web.UI.WebControls.DataGridPageChangedEventHandler 
(this.DataGrid1_PageIndexChanged); 
this.Load += new System.EventHandler(this.Page_Load); 


#endregion 

private void DataGrid1_PageIndexChanged(object source, 
System.Web.UI.WebControls.DataGridPageChangedEventArgs e) 

//設置DataGrid當前頁的索引值為用戶選擇的頁的索引 
DataGrid1.CurrentPageIndex=e.NewPageIndex; 
//取得當前頁為止總共有多少條記錄,以便在下一頁就從該記錄開始讀取 
startIndex=DataGrid1.PageSize*DataGrid1.CurrentPageIndex; 
//取得綁定數據 
Bind(); 



在這段程序中,首先在數據綁定時,設置DataGrid控件的VirtualItemCoun屬性值為查詢結果集中的 
記錄總數,然后取得當前頁要顯示的數據,初始化時當前頁顯示的數據為從取到的數據的零位置開始,到 
DataGrid控件的PageSize屬性的設定值為止的數據記錄數.在分頁操作中重新定義了取得下一頁數據時 
的數據項的開始索引值,然后調用數據綁定方法把取到的新數據和DataGrid控件重新綁定.
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 穆棱市| 若羌县| 玉溪市| 温宿县| 揭东县| 孝义市| 伽师县| 开鲁县| 铜鼓县| 常山县| 灵丘县| 上虞市| 车险| 秀山| 潢川县| 四川省| 喀喇沁旗| 根河市| 深圳市| 五寨县| 开平市| 宁安市| 邵阳市| 阿坝县| 西城区| 马边| 岳普湖县| 田阳县| 舞阳县| 荔波县| 龙口市| 青浦区| 九龙城区| 西林县| 故城县| 江永县| 获嘉县| 沾益县| 益阳市| 深水埗区| 铜山县|