Asp.Net北大青鸟总结(四)-使用GridView实现真假分页

protected void Page_Load(object sender, EventArgs e) { //页面第一次载入 if (!Page.IsPostBack ) { //绑定数据 GridView1.DataSource = MyData(); GridView1.DataBind(); } } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; DataTable dt = MyData(); GridView1.DataSource = dt; GridView1.DataBind(); } private static DataTable MyData() { SqlConnection con = new SqlConnection(server=.;database=northwind;uid=sa;password=gss123); con.Open(); string strCmd = select * from comment; SqlCommand cmd = new SqlCommand(strCmd,con ); SqlDataReader sdr = cmd.ExecuteReader(); DataTable dt=new DataTable (); dt.Load(sdr ); con.Close(); return dt; } }
以上就是假分页的数据绑定。

2.真分页

真分页但是要花一点功夫的,真分页是须要aspnetpaper这个第三方控件的,须要下载他的DLL文件然后在拖动至工具箱的然后在使用它,他就是一个翻页的工具

 protected void Page_Load(object sender, EventArgs e)  
        {  
            if (!Page.IsPostBack )  
            {  
                //窗口打卡时,起始数据编号为0,终止数据编号为每页的信息条数  
                int intStartIndex = ANP.PageSize * 0;  
                int intEndIndex = ANP.PageSize * 1;  
              span style=color:#ff0000;strong  ANP.RecordCount = MyAllData().Rows.Count;/strong/span  
                //绑定数据  
                GridView1.DataSource = MyPartData(intStartIndex, intEndIndex);  
                GridView1.DataBind();  
            }  
        }  
        /// summary  
        /// 改变了页数的数据显示  
        /// /summary  
        /// param name=sender/param  
        /// param name=e/param  
        protected void ANP_PageChanged(object sender, EventArgs e)  
        {  
            //起始数据编号=每页的信息容量*(当前页数-1)+1。  
            //例:第六页,每页显示5条,第六页的起始数据=5*5+1=26;  
            int intStartIndex=ANP.PageSize * (ANP.CurrentPageIndex-1)+1;  
            int intEndIndex = ANP.PageSize * ANP.CurrentPageIndex;  
            GridView1.DataSource = MyPartData(intStartIndex,intEndIndex );  
            GridView1.DataBind();  
  
        }  
        /// summary  
        /// 分页查询  
        /// /summary  
        /// param name=intStartIndex開始的数据编号/param  
        /// param name=intEndIndex结束的数据编号/param  
        /// returns表格/returns  
        private static DataTable MyPartData(int intStartIndex,int intEndIndex)  
        {  
            SqlConnection con = new SqlConnection(server=.;database=newssystem;uid=sa;password=123456);  
            con.Open();  
            string strCmd = select * from comment;  
            SqlCommand cmd = new SqlCommand(strCmd,con );  
            SqlDataReader sdr = cmd.ExecuteReader();  
            DataTable dt=new DataTable ();  
            dt.Load(sdr );  
            con.Close();  
            return dt;  
  
        }  
        /// summary  
        /// 所有查询  
        /// /summary  
        /// returns/returns  
        private static DataTable MyAllData()  
        {  
            SqlConnection con = new SqlConnection(server=.;database=northwind;uid=sa;password=gss123);  
            con.Open();  
            string Cmd = select * from comment;  
            SqlCommand cmd = new SqlCommand(Cmd, con);  
            SqlDataReader sdr = cmd.ExecuteReader();  
            DataTable dt = new DataTable();  
            dt.Load(sdr);  
            con.Close();  
            return dt;  
  
        }  
    }  

经过这段学习我了解了关于真假分页的事儿所以,以后还要继续学习他的一些东西,还有我明确了有时候自己走不通的时候能够看看别人是怎么做的,可是不要全然的借鉴,也要有自己的体会。



Asp.Net北大青鸟总结(四)-使用GridView实现真假分页

原文地址:http://www.cnblogs.com/liguangsunls/p/7267970.html


最新回复(0)
/jishu11Vr3LEyeZoQuyaCyVcDx2H1MCKvevk8Ig81Fw_3D_3D4719290
8 简首页