/// <summary> /// 塗聚文 2011-10-24 (參考相關網絡資料) /// 締友計算機信息技術有限公司 ///C# Winform 簡單分頁: 首頁,上一頁,下一頁,末頁 /// </summary> public partial class FormDataGridViewPage : Form { /// <summary> /// 每页显示行数 /// </summary> int pageSize = 0; /// <summary> /// 总记录数 /// </summary> int nMax = 0; /// <summary> /// 页数=总记录数/每页显示行数 /// </summary> int pageCount = 0; /// <summary> /// 当前页号 /// </summary> int pageCurrent = 0; /// <summary> /// 当前记录行 /// </summary> int nCurrent = 0; DataSet ds = new DataSet(); DataTable dtInfo = new DataTable(); AdministrativeAreaListBLL administrativeAreaListBLL = new AdministrativeAreaListBLL(); AdministrativeAreaListInfo administrativeAreaListInfo = new AdministrativeAreaListInfo(); FormOperatingStringInfo formOperatingStringInfo = new FormOperatingStringInfo(); /// <summary> /// /// </summary> public FormDataGridViewPage() { InitializeComponent(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FormDataGridViewPage_Load(object sender, EventArgs e) { dtInfo = administrativeAreaListBLL.SelectAdministrativeAreaListDataTable(); InitDataSet(); } /// <summary> /// 用首页数据填充 /// 塗聚文 /// 締友計算機信息技術有限公司 ///C# Winform 簡單分頁: 首頁,上一頁,下一頁,末頁 /// </summary> private void InitDataSet() { pageSize = 20; //设置页面行数 nMax = dtInfo.Rows.Count; pageCount = (nMax / pageSize); //计算出总页数 if ((nMax % pageSize) > 0) pageCount++; pageCurrent = 1; //当前页数从1开始 nCurrent = 0; //当前记录数从0开始 LoadData(); } /// <summary> /// 用末頁數據填育 /// 塗聚文 /// 締友計算機信息技術有限公司 ///C# Winform 簡單分頁: 首頁,上一頁,下一頁,末頁 /// </summary> private void EndDataSetPage() { pageSize = 20; //设置页面行数 nMax = dtInfo.Rows.Count; pageCount = (nMax / pageSize); //计算出总页数 int rou = nMax % pageSize;//取数 if (rou > 0) { nCurrent = nMax - (nMax % pageSize);//不是正好整除页码,取余数页 pageCount++; } else { nCurrent = nMax - pageSize; } pageCurrent = pageCount; LoadData(); } /// <summary> /// 填充DataGridView /// 塗聚文 /// 締友計算機信息技術有限公司 ///C# Winform 簡單分頁: 首頁,上一頁,下一頁,末頁 /// </summary> private void LoadData() { int nStartPos = 0; //当前页面开始记录行 int nEndPos = 0; //当前页面结束记录行 DataTable dtTemp = dtInfo.Clone(); //克隆DataTable结构框架 if (pageCurrent == pageCount) { nEndPos = nMax; } else { nEndPos = pageSize * pageCurrent; } nStartPos = nCurrent; lblPageCount.Text = "/"+pageCount.ToString(); //共多少頁 txtCurrentPage.Text = Convert.ToString(pageCurrent);//當前頁 //从元数据源复制记录行 for (int i = nStartPos; i < nEndPos; i++) { dtTemp.ImportRow(dtInfo.Rows[i]); nCurrent++; } bindingSourceInfo.DataSource = dtTemp; bindingNavigatorInfo.BindingSource = bindingSourceInfo; DataGridViewInfo.DataSource = bindingSourceInfo; } /// <summary> /// 点击的选择页操作 /// 涂聚文 /// 締友計算機信息技術有限公司 ///C# Winform 簡單分頁: 首頁,上一頁,下一頁,末頁 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void bindingNavigatorInfo_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { if (e.ClickedItem.Text == "关闭") { this.Close(); } if (e.ClickedItem.Text == "首页") { pageCurrent = 1; pageSize = 20; InitDataSet(); } if (e.ClickedItem.Text == "末页") { EndDataSetPage(); } if (e.ClickedItem.Text == "上一页") { pageCurrent--; if (pageCurrent <= 0) { MessageBox.Show("已经是第一页,请点击“下一页”查看!"); return; } else { nCurrent = pageSize * (pageCurrent - 1); } LoadData(); } if (e.ClickedItem.Text == "下一页") { pageCurrent++; if (pageCurrent > pageCount) { MessageBox.Show("已经是最后一页,请点击“上一页”查看!"); return; } else { nCurrent = pageSize * (pageCurrent - 1); } LoadData(); } } }