日期:2014-05-17  浏览次数:20422 次

手写分页,求原码?解决后立刻给分。分不多了见谅
我平常用gridview来绑定dataset从来也没有用过datatable,所以datatable从来没有用过更别谈分页了。
想求一个源代码,给datatable绑定数据,分页,实现增删改查像gridview那样的。解决后立刻结贴。


------解决方案--------------------
控件分页的
C# code

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.IO;
using System.Drawing;

public partial class display_display : System.Web.UI.Page
{
    int CurrentPage;//当前页数
    int PageSize;   //每页条数
    int PageCount;  //总页数
    int RecordCount;//总条数
    public string SubStr(string sString, int nLeng)
    {
        if (sString.Length <= nLeng)
        {
            return sString;
        }
        string sNewStr = sString.Substring(0, nLeng);
        sNewStr = sNewStr + "...";
        return sNewStr;
    }
    protected string FormatURL(object strArgument)
    {
        return "image.aspx?id=" + strArgument.ToString();
    }
    private void Page_Load(object sender, System.EventArgs e)
    {
        string pic = Request["Pic_ID"];

        PageSize = 9;//每页10条记录


        if (!Page.IsPostBack)
        {
            CurrentPage = 0;//当前页习惯设为0
            ViewState["PageIndex"] = 0;//页索引也设为0


            //计算总共有多少记录
            RecordCount = CalculateRecord();


            //计算总共有多少页
            if (RecordCount % PageSize == 0)
            {
                PageCount = RecordCount / PageSize;
            }
            else
            {
                PageCount = RecordCount / PageSize + 1;
            }

            this.TotalLbl.Text = PageCount.ToString();//显示总页数
            ViewState["PageCount"] = PageCount;//会话session 对整个 application 有效 ,而视图状态 viewstate相当于某个页面的 session

            this.DataListBind();//不可以放在初始化条件之前就绑定,那样的话,如果仅有一页的数据,“下一页”页仍然显示

        }


    }


    //计算总共有多少条记录
    private int CalculateRecord()
    {
        try
        {
            int recordCount;
            OleDbConnection oc = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath(@"~\App_Data\DataBase.aspx")); //数据库使用Northwind;
            oc.Open();
            string OleDb = "select count(*) as [count] from Picture";
            OleDbCommand cmd = new OleDbCommand(OleDb, oc);
            OleDbDataReader sdr = cmd.ExecuteReader();

            if (sdr.Read())
            {
                recordCount = Int32.Parse(sdr["count"].ToString());
            }


            else
            {
                recordCount = 0;
            }

            sdr.Close();
            oc.Close();
            return recordCount;
        }


        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }


    //将数据绑定到Datalist控件
    public void DataListBind()
    {
        try
        {
            int StartIndex = CurrentPage * PageSize;//设定导入的起终地址
            string OleDb = "SELECT [Pic_ID], [Pic],[Pic_Title] FROM [Picture] order by [Pic_id] desc";
            DataSet ds = new DataSet();
            OleDbConnection oc = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath(@"~\App_Data\DataBase.aspx"));
            oc.Open();

            OleDbDataAdapter sda = new OleDbDataAdapter(OleDb, oc);
            sda.Fill(ds, StartIndex, PageSize, "Picture");//这是sda.Fill方法的第一次重载,里面的变量分别是数据集DataSet ,开始记录数StartRecord,最大的记录数MaxRecord,数据表名TableName
            this.DataList1.DataSource = ds.Tables["Picture"].DefaultView;
            this.DataList1.DataBind();
            this.PreviousLB.Enabled = true;
            this.NextLB.Enabled = true;
            if (CurrentPage == (PageCount - 1)) this.NextLB.Enabled = false;//当为最后一页时,下一页链接