基于linq2sql的分页控件(自己写的)
找了好多,都是不带datagridview的。
所以自己写类一个。。。
源码如下
View Code
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.Sql; namespace 分页控件 { /*Made by Anby * 2012-10-18 * Good night! */ public partial class DGVPaging : UserControl { private int pagesize = 10; //每页行数 private int pagecurr = 1; // 当前页 private int pagecounts = 0; //总页数 private int rowcounts = 0; //总行数 public DataTable DT = new DataTable(); #region 属性 /// <summary> /// dataGridView的DataSource属性 /// </summary> [Browsable(true)] [Category("数据")] [Bindable(true)] public DataTable DataSource { get { return (DataTable)dataGridView1.DataSource; } set { dataGridView1.DataSource =value ; } } /// <summary> /// 给DT赋值 /// </summary> public DataTable data { get { return DT;} set { DT=value;} } /// <summary> /// 每页行数 /// </summary> public int PageSize { get { return pagesize; } set { pagesize = value; } } /// <summary> /// 当前页 /// </summary> public int PageCurr { get { return pagecurr; } set { currPagetxt.Text = pagecurr.ToString(); pagecurr = value; } } /// <summary> /// 总页数 /// </summary> public int PageCounts { get { if (pagesize != 0) { pagecounts = GetPageCount(); } txtPageSize.Text = pagecounts.ToString(); return pagecounts; } } //总行数 public int RowCounts { get { rowcounts = DT.Rows.Count; return rowcounts; } } #endregion #region 构造函数 public DGVPaging() { InitializeComponent(); } #endregion #region 方法 public int GetPageCount() { if (PageSize == 0) { return 0; } int pageCount = RowCounts / PageSize; if (RowCounts % PageSize == 0) pageCount = RowCounts / PageSize; if (RowCounts % PageSize > 0) pageCount = RowCounts / PageSize + 1; return pageCount; } private void SetBtnEnable() { firPagelbtn.Enabled = true; prvPagelbtn.Enabled = true; nexPagelbtn.Enabled = true; lastPagelbtn.Enabled = true; } /// <summary> /// 当pagesize小于rowcounts时,首页使用此方法" /// </summary> public void GoFirst() { DataSet dstemp = new DataSet(); IEnumerable<DataRow> x = from result in DT.AsEnumerable() select result; var y = x.Take(PageSize); DataRow[] Rows = new DataRow[x.Count()]; int j = 0; foreach (var item in y) { if (j < Rows.Length) { Rows[j] = item; j++; } else { break; } } dstemp.Merge(Rows); dataGridView1.DataSource = dstemp.Tables[0]; firPagelbtn.Enabled = false; prvPagelbtn.Enabled = false; nexPagelbtn.Enabled = false; lastPagelbtn.Enabled = false; } /// <summary> /// 转到首页 /// </summary> public void GoFirstPage() { SetBtnEnable(); DataSet dstemp = new DataSet(); IEnumerable<DataRow> x = from result in DT.AsEnumerable() select result; if (PageCounts > 1) { var y= x.Take(PageSize); DataRow[] Rows = new DataRow[y.Count()]; int j = 0; foreach (var item in y) { if (j < Rows.Length) { Rows[j] = item; j++; } else { break; } } dstemp.Merge(Rows); } else { var y = x.Take(PageSize); DataRow[] Rows = new DataRow[y.Count()]; int j = 0; foreach (var item in y) { if (j < Rows.Length) { Rows[j] = item; j++; } else { break; } } dstemp.Merge(Rows); } dataGridView1.DataSource = dstemp.Tables[0]; PageCurr = 1; firPagelbtn.Enabled = false; prvPagelbtn.Enabled = false; currPagetxt.Text = PageCurr.ToString(); } /// <summary> /// 转到上一页 /// </summary> private void GoPrvPage() { SetBtnEnable(); DataSet dstemp = new DataSet(); IEnumerable<DataRow> x = from result in DT.AsEnumerable() select result; if (PageCurr != 2) { var y= x.Skip((PageCurr - 2) * PageSize).Take(PageSize); DataRow[] Rows = new DataRow[y.Count()]; int j = 0; foreach (var item in y) { if (j < Rows.Length) { Rows[j] = item; j++; } else { break; } } dstemp.Merge(Rows); dataGridView1.DataSource = dstemp.Tables[0]; PageCurr--; currPagetxt.Text = PageCurr.ToString(); } else { GoFirstPage(); } } /// <summary> /// 转到最后一页 /// </summary> private void GoLastPage() { SetBtnEnable(); DataSet dstemp = new DataSet(); IEnumerable<DataRow> x = from result in DT.AsEnumerable() select result; var y = x.Skip((PageCounts - 1) * PageSize).Take(RowCounts % PageSize == 0 ? PageSize : RowCounts % PageSize); DataRow[] Rows = new DataRow[y.Count()]; int j = 0; foreach (var item in y) { if (j < Rows.Length) { Rows[j] = item; j++; } else { break; } } dstemp.Merge(Rows); dataGridView1.DataSource = dstemp.Tables[0]; PageCurr = PageCounts; nexPagelbtn.Enabled = false; lastPagelbtn.Enabled = false; currPagetxt.Text = PageCurr.ToString(); } /// <summary> /// 转到下一页 /// </summary> private void GoNextPage() { SetBtnEnable(); DataSet dstemp = new DataSet(); IEnumerable<DataRow> x = from result in DT.AsEnumerable() select result; if (PageCurr!=PageCounts-1) { var y=x.Skip(PageCurr* PageSize).Take(PageSize); DataRow[] Rows = new DataRow[y.Count()]; int j = 0; foreach (var item in y) { if (j < Rows.Length) { Rows[j] = item; j++; } else { break; } } dstemp.Merge(Rows); dataGridView1.DataSource = dstemp.Tables[0]; PageCurr++; currPagetxt.Text = PageCurr.ToString(); } else { GoLastPage(); } } int temp=0; /// <summary> /// 跳转到txtPageNum.text所显示的页面 /// </summary> public void GoAnyPage() { if (txtPageNum.Text.Trim() == "1") { if (RowCounts < PageSize) GoFirst(); else GoFirstPage(); } else if (txtPageNum.Text.Trim() == PageCounts.ToString()) { GoLastPage(); } else { if (int.TryParse(txtPageNum.Text.Trim(),out temp) == true) { PageCurr = int.Parse(txtPageNum.Text.Trim()); SetBtnEnable(); DataSet dstemp = new DataSet(); IEnumerable<DataRow> x = from result in DT.AsEnumerable() select result; var y = x.Skip((PageCurr-1) * PageSize).Take(PageSize); DataRow[] Rows = new DataRow[y.Count()]; int j = 0; foreach (var item in y) { if (j < Rows.Length) { Rows[j] = item; j++; } else { break; } } dstemp.Merge(Rows); dataGridView1.DataSource = dstemp.Tables[0]; currPagetxt.Text = PageCurr.ToString(); } } } #endregion private void firPagelbtn_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { firPagelbtn.Enabled = false; if (RowCounts < PageSize)//如果总行数小于规定的页面行数 GoFirst(); else GoFirstPage(); } private void prvPagelbtn_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { prvPagelbtn.Enabled = false; GoPrvPage(); } private void nexPagelbtn_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { nexPagelbtn.Enabled = false; GoNextPage(); } private void lastPagelbtn_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { lastPagelbtn.Enabled = false; GoLastPage(); } private void btnGO_Click(object sender, EventArgs e) { try { int go2page = int.Parse(txtPageNum.Text.Trim()); if (txtPageNum.Text.Trim() == "") MessageBox.Show("Sorry,Pls input which page you will Go"); if (go2page > PageCounts) MessageBox.Show("Sorry,RowCount is " + PageCounts); if (go2page < 1) MessageBox.Show("Sorry,Min of the page is 1"); if (int.TryParse(txtPageNum.Text.Trim(), out temp) == true) { GoAnyPage(); } } catch (Exception ex) { } } } }
使用如下
View Code
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Windows.Forms; namespace test { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { DataTable dt=new DataTable(); dt.Columns.Add("姓名",typeof(string)); dt.Rows.Add("LiLei1"); dt.Rows.Add("LiLei2"); dt.Rows.Add("LiLei3"); dt.Rows.Add("LiLei4"); dt.Rows.Add("LiLei5"); dt.Rows.Add("LiLei6"); dt.Rows.Add("LiLei7"); dt.Rows.Add("LiLei8"); dt.Rows.Add("LiLei9"); dt.Rows.Add("LiLei10"); dt.Rows.Add("LiLei11"); dt.Rows.Add("LiLei12"); dt.Rows.Add("LiLei13"); dt.Rows.Add("LiLei14"); dt.Rows.Add("LiLei15"); dt.Rows.Add("LiLei16"); dt.Rows.Add("LiLei17"); dt.Rows.Add("LiLei18"); dt.Rows.Add("LiLei19"); dt.Rows.Add("LiLei20"); dt.Rows.Add("LiLei21"); dt.Rows.Add("LiLei22"); dt.Rows.Add("LiLei23"); dt.Rows.Add("LiLei24"); dt.Rows.Add("LiLei25"); dt.Rows.Add("LiLei26"); dt.Rows.Add("LiLei27"); dt.Rows.Add("LiLei28"); dt.Rows.Add("LiLei29"); dt.Rows.Add("LiLei30"); dt.Rows.Add("LiLei31"); dt.Rows.Add("LiLei32"); dgvPaging1.PageSize = 4; int PageSize = dgvPaging1.PageSize; dgvPaging1.data = dt; if (dt.Rows.Count < PageSize) dgvPaging1.GoFirst(); else dgvPaging1.GoFirstPage(); } } }