1. 程式人生 > >C# 資料庫分頁顯示類

C# 資料庫分頁顯示類

實現效果圖
using System.Data;
using System.Windows.Forms;

namespace WeightSystem
{
class Page
{
public void Load(int pagesize, int recordcount, int pagecount, int currentpage, DataTable table
,DataGridView dgvtable,TextBox txtcurrentpage, TextBox txtpagecount, TextBox txtrecordcount, TextBox txtpagesize)
{
if (currentpage < 1) currentpage = 1;
if (currentpage > pagecount) currentpage = pagecount;

        int beginRecord;
        int endRecord;
        DataTable dtTemp;
        dtTemp = table.Clone();

        beginRecord = pagesize * (currentpage - 1);
        if (currentpage == 1) beginRecord = 0;
        endRecord = pagesize * currentpage;

        if (currentpage == pagecount) endRecord = recordcount;
        for (int i = beginRecord; i < endRecord; i++)
        {
            if (table.Rows.Count > 0)
            {
                dtTemp.ImportRow(table.Rows[i]);
            }
        }
        dgvtable.DataSource = dtTemp;  //datagridview控制元件繫結
        txtcurrentpage.Text = currentpage.ToString();//當前頁
        txtpagecount.Text = pagecount.ToString();//總頁數
        txtrecordcount.Text = recordcount.ToString();//總記錄數
        txtpagesize.Text = pagesize.ToString();//每頁的條數
    }
}

}