.Net 分页实现系列之一 DataGrid本身提供的分页方式

  基于上次看了CodeProject的一篇文章后,
 很多刚学习.net的朋友们都向我咨询一些该如何分页的问题.于是萌发想写一篇教导初学者如何分页的文章

 1、  DataGrid本身提供的分页方式

     该方式简单易用,缺点效率有点差

   实现方法如下

第一步,从工具箱Web窗体〉拖拉出DataGrid控件,设置其属性

AllowPaging="True"

PageSize="20"

Html代码效果如下

 ArrayManage.aspx

<asp:DataGrid id="DataGrid1" runat="server" CssClass="SolidDataGrid" singleValue="#F7F7F7" oldValue="#FFFFFF" DataKeyField="GroupId" Width="98%" AutoGenerateColumns="False" AllowPaging="True" PageSize="20">

<PagerStyle Mode="NumericPages"></PagerStyle>

</asp:DataGrid>

 

第二步 双击该窗体,进入codebehide

 private void Page_Load(object sender, System.EventArgs e)

         {

              // 在此处放置用户代码以初始化页面

                   if(!Page.IsPostBack)

              {

                       BindData();

              }

         }

 private void BindData()

         {

            SqlConnection Conn = new SqlConnection();

              Conn.ConnectionString =@"server=MIS\PMSERVER;uid=sa;pwd=sa;database=test";

                   Conn.Open();

                   string strSql = "select  typeId,typename from type_Info ";

                   DataSet ds = new DataSet();

                   SqlDataAdapter Adp = new SqlDataAdapter(strSql,Conn);

                   Adp.Fill(ds,"TypeIdList");

                this.DataGrid1.DataSource= ds.Tables[0];

               this.DataGrid1.DataBind();

         }

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

         {

              DataGrid1.CurrentPageIndex = e.NewPageIndex;

              BindData();
}

原文地址:https://www.cnblogs.com/meetweb/p/316630.html