问题描述
asp.net如何实现datagrid的分页显示使用的是VS2003下VB.net来开发的望指教,谢谢
解决方案
解决方案二:
设置AllowPaging=true;
解决方案三:
<asp:datagridid="DataGrid1"runat="server"CellSpacing="0"CellPadding="2"PagerStyle-HorizontalAlign="Right"PageSize="3"AllowPaging="True"pagerstyle-nextpagetext="下一页"pagerstyle-prevpagetext="上一页"onpageindexchanged="changeage"></asp:datagrid>然后在后台代码加入subchangepage(sasobject,easdatagridpagechangedeventargs)datagrid1.currentpageindex=e.newpageindexendsub
解决方案四:
用自定义分页,然后按页码写一个绑定的函数去获取DT绑定
解决方案五:
给你一个自定义分页的例子.aspx<%@Pagelanguage="c#"Codebehind="DataGridCustomPage.aspx.cs"AutoEventWireup="false"Inherits="CommonFunction.DataGridCustomPage"%><!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN"><HTML><HEAD><title>DataGridCustomPage</title><metaname="GENERATOR"Content="MicrosoftVisualStudio.NET7.1"><metaname="CODE_LANGUAGE"Content="C#"><metaname="vs_defaultClientScript"content="JavaScript"><metaname="vs_targetSchema"content="http://schemas.microsoft.com/intellisense/ie5"></HEAD><bodyMS_POSITIONING="GridLayout"><formid="Form1"method="post"runat="server"><h2>DataGrid自定义分页的例子</h2><asp:datagridid="dgCustomPage"style="Z-INDEX:101;LEFT:16px;POSITION:absolute;TOP:48px"runat="server"Width="376px"AutoGenerateColumns="False"CellPadding="4"BackColor="White"BorderWidth="1px"BorderStyle="None"BorderColor="#3366CC"Height="20px"AllowPaging="True"AllowCustomPaging="True"PageSize="5"><SelectedItemStyleFont-Bold="True"ForeColor="#CCFF99"BackColor="#009999"></SelectedItemStyle><ItemStyleForeColor="#003399"BackColor="White"></ItemStyle><HeaderStyleFont-Bold="True"ForeColor="#CCCCFF"BackColor="#003399"></HeaderStyle><FooterStyleForeColor="#003399"BackColor="#99CCCC"></FooterStyle><Columns><asp:BoundColumnDataField="LastName"HeaderText="姓名"></asp:BoundColumn><asp:BoundColumnDataField="FirstName"HeaderText="姓"></asp:BoundColumn><asp:BoundColumnDataField="City"HeaderText="城市"></asp:BoundColumn><asp:BoundColumnDataField="BirthDate"HeaderText="出生年月"DataFormatString="{0:D}"></asp:BoundColumn></Columns><PagerStyleHorizontalAlign="Left"ForeColor="#003399"BackColor="#99CCCC"Mode="NumericPages"></PagerStyle></asp:datagrid></form></body></HTML>.CSusingSystem;usingSystem.Collections;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Web;usingSystem.Web.SessionState;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.HtmlControls;usingSystem.Data.SqlClient;usingSystem.Configuration;namespaceCommonFunction{///<summary>///DataGridCustomPage的摘要说明。///</summary>publicclassDataGridCustomPage:System.Web.UI.Page{protectedSystem.Web.UI.WebControls.DataGriddgCustomPage;//定义全局变量用来保存每页的起始项索引intstartIndex=0;privatevoidPage_Load(objectsender,System.EventArgse){//页面初试化时进行数据绑定if(!IsPostBack)DataGridDataBind();}privatevoidDataGridDataBind(){//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的SqlConnectionconn=newSqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());//创建数据适配器对象SqlDataAdapterda=newSqlDataAdapter("selectLastName,FirstName,BirthDate,CityfromEmployees",conn);//创建DataSet对象DataSetds=newDataSet();try{//从指定的索引开始取PageSize条记录da.Fill(ds,startIndex,dgCustomPage.PageSize,"CurDataTable");//填充数据集da.Fill(ds,"AllDataTable");//设置DataGrid控件实际要显示的项数dgCustomPage.VirtualItemCount=ds.Tables["AllDataTable"].Rows.Count;//进行数据绑定dgCustomPage.DataSource=ds.Tables["CurDataTable"];dgCustomPage.DataBind();}catch(Exceptionerror){Response.Write(error.ToString());}}#regionWeb窗体设计器生成的代码overrideprotectedvoidOnInit(EventArgse){////CODEGEN:该调用是ASP.NETWeb窗体设计器所必需的。//InitializeComponent();base.OnInit(e);}///<summary>///设计器支持所需的方法-不要使用代码编辑器修改///此方法的内容。///</summary>privatevoidInitializeComponent(){this.dgCustomPage.PageIndexChanged+=newSystem.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgCustomPage_PageIndexChanged);this.Load+=newSystem.EventHandler(this.Page_Load);}#endregionprivatevoiddgCustomPage_PageIndexChanged(objectsource,System.Web.UI.WebControls.DataGridPageChangedEventArgse){//设置DataGrid当前页的索引值为用户选择的页的索引dgCustomPage.CurrentPageIndex=e.NewPageIndex;//取得当前页为止总共有多少条记录,以便在下一页就从该记录开始读取startIndex=dgCustomPage.PageSize*dgCustomPage.CurrentPageIndex;//重新绑定数据DataGridDataBind();}}}
解决方案六:
第一.使用DataGrid的自带的分页控件第二.使用AspNetPager分页控件第三.自己写分页参考http://www.cnblogs.com/sunnystar365/archive/2005/09/28/245665.html