在winform中,分頁有時候也是必不可少的一項,是以, 新手mark一下有時候是必要的。
下面開始簡單介紹一下:
既然是分頁控件,說明它是個控件,是以,繼承控件繼承自使用者控件如下:
public partial class pagercontrol : usercontrol
{
#region 構造函數
public pagercontrol()
{
initializecomponent();
}
}
然後就是對界面拉一些控件了,如下圖:
各個控件的id就不一個個打了,下面的代碼看名稱就知道了。
按下來定義幾個屬性(第幾頁,每頁幾條,記錄總數,這幾個是國際性慣例必須的)
public partial class pagercontrol : usercontrol
{
#endregion
#region 分頁字段和屬性
private int pageindex = 1;
/// <summary>
/// 目前頁面
/// </summary>
public virtual int pageindex
get { return pageindex; }
set { pageindex = value; }
private int pagesize = 100;
/// 每頁記錄數
public virtual int pagesize
get { return pagesize; }
set { pagesize = value; }
private int recordcount = 0;
/// 總記錄數
public virtual int recordcount
get { return recordcount; }
set { recordcount = value; }
private int pagecount = 0;
/// 總頁數
public int pagecount
get
{
if (pagesize != 0)
{
pagecount = getpagecount();
}
return pagecount;
}
/// <summary>
/// 計算總頁數
/// </summary>
/// <returns></returns>
private int getpagecount()
{
if (pagesize == 0)
{
return 0;
}
int pagecount = recordcount / pagesize;
if (recordcount % pagesize == 0)
pagecount = recordcount / pagesize;
else
pagecount = recordcount / pagesize + 1;
return pagecount;
}
#endregion
上面順帶多了一個計算頁總數的代碼。
接着定義一個事件,用于在引發分頁時,友善外部重新擷取資料綁定:
public event eventhandler onpagechanged;
再往下就是點選按鈕的分頁和引發的控件重繪了:
看“首頁,上一頁,下一頁,尾頁”事件:
private void lnkfirst_linkclicked(object sender, linklabellinkclickedeventargs e)
pageindex = 1;
drawcontrol(true);
private void lnkprev_linkclicked(object sender, linklabellinkclickedeventargs e)
pageindex = math.max(1, pageindex - 1);
private void lnknext_linkclicked(object sender, linklabellinkclickedeventargs e)
pageindex = math.min(pagecount, pageindex + 1);
private void lnklast_linkclicked(object sender, linklabellinkclickedeventargs e)
pageindex = pagecount;
}
代碼是在分頁類裡,這裡就不重複再寫上面的寫過的代碼了。
然後是重繪控件的代碼:
/// <summary>
/// 外部調用
public void drawcontrol(int count)
recordcount = count;
drawcontrol(false);
/// 頁面控件呈現
private void drawcontrol(bool callevent)
btngo.text = jumptext;
lblcurrentpage.text = pageindex.tostring();
lblpagecount.text = pagecount.tostring();
lbltotalcount.text = recordcount.tostring();
txtpagesize.text = pagesize.tostring();
if (callevent && onpagechanged != null)
onpagechanged(this, null);//目前分頁數字改變時,觸發委托事件
setformctrenabled();
if (pagecount == 1)//有且僅有一頁
lnkfirst.enabled = false;
lnkprev.enabled = false;
lnknext.enabled = false;
lnklast.enabled = false;
btngo.enabled = false;
else if (pageindex == 1)//第一頁
else if (pageindex == pagecount)//最後一頁
private void setformctrenabled()
lnkfirst.enabled = true;
lnkprev.enabled = true;
lnknext.enabled = true;
lnklast.enabled = true;
btngo.enabled = true;
ok,分頁的代碼基本就完了,為了提升一點小使用者體驗,包括改變分頁大小,或在輸入跳轉頁後按加車也能執行事件,這裡加多一點處理代碼:
/// enter鍵功能
private void txtpagenum_keypress(object sender, keypresseventargs e)
btngo_click(null, null);
/// 跳轉頁數限制
private void txtpagenum_textchanged(object sender, eventargs e)
int num = 0;
if (int.tryparse(txtpagenum.text.trim(), out num) && num > 0)
if (num > pagecount)
txtpagenum.text = pagecount.tostring();
/// 跳轉
/// <param name="sender"></param>
/// <param name="e"></param>
private void btngo_click(object sender, eventargs e)
pageindex = num;
drawcontrol(true);
bool istextchanged = false;
/// 分頁屬性改變了。
private void txtpagesize_textchanged(object sender, eventargs e)
if (!int.tryparse(txtpagesize.text.trim(), out num) || num <= 0)
num = 100;
txtpagesize.text = "100";
else
istextchanged = true;
pagesize = num;
/// 光标離開分頁屬性
private void txtpagesize_leave(object sender, eventargs e)
if (istextchanged)
istextchanged = false;
lnkfirst_linkclicked(null, null);
到這裡,分頁的源碼就寫完了,各位自己有空也多寫寫,就那麼幾個事件和步驟。
控件出來了,接下就是弄個簡單的界面示例意思意思一下,然後提供下源碼了。
建立winform項目、往裡拖一個datagridview+剛才的分頁控件,如下圖:
private void startform_load(object sender, eventargs e)
//建立檔案資料庫表。
mdatatable.createschema("users.txt", false, new string[] { "username", "password", "enabled" }, sqldbtype.nvarchar, sqldbtype.nvarchar, sqldbtype.bit);
for (int i = 0; i < 200; i++)//插入200條資料。
using (maction action = new maction("users.txt", "txt path={0}"))
action.set("username", "username_" + i);
action.set("password", "password_" + i);
action.set("enabled", i%2==0);
action.insert(insertop.none);
pagercontrol1.onpagechanged += new eventhandler(pagercontrol1_onpagechanged);
loaddata();
void pagercontrol1_onpagechanged(object sender, eventargs e)
void loaddata()
int count;
using (maction action = new maction("users.txt", "txt path={0}"))
action.select(pagercontrol1.pageindex, pagercontrol1.pagesize, string.empty, out count).bind(gvusers);
pagercontrol1.drawcontrol(count);
代碼很簡單,重點在
pagercontrol1.onpagechanged事件裡調用loaddata來綁定資料,重繪分布控件時調用
pagercontrol1.drawcontrol(傳進記錄總數);
于是資料出來了,效果如下圖:
版權聲明:本文原創發表于部落格園,作者為路過秋天,原文連結:
http://www.cnblogs.com/cyq1162/archive/2012/06/12/2545764.html
<a href="http://files.cnblogs.com/cyq1162/winpager.rar"></a>