完整代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsFormsDGVFenYe { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //源数据 private DataTable dt_sourceData; //当前页码 private int m_currentPage; //总页数 private int ZongYeShu; //每页显示的项数 private int m_itemsPerPage; //总项数 private int ZongXiangShu; //状态 private enum PageState { Init = 0, Pre = -1, Next = 1 } private void Form1_Load(object sender, EventArgs e) { InitData();//初始化 GetData();//获取数据 } #region 初始化 private void InitData() { dt_sourceData = null; m_currentPage = 1;//当前页数 m_itemsPerPage = 10;//每页总项数 ZongXiangShu = 0; ZongYeShu = 0; } #endregion #region 刷新数据 private void GetData() { string strCon = "server=.;database=HSPB;uid=sa;pwd=123;"; SqlConnection Conn = new SqlConnection(strCon); Conn.Open(); string strSql = "SELECT XingMing, XingBie, XueXing, ChuShengNianYue FROM XiTong_YuanGongXinXi"; SqlCommand cmd = new SqlCommand(strSql, Conn); SqlDataAdapter da = new SqlDataAdapter(cmd); //SqlDataAdapter da = new SqlDataAdapter(strSql, Conn);//定义dataadapter DataSet ds = new DataSet(); da.Fill(ds); //dataGridView1.DataSource = ds.Tables[0]; SetSourceData(ds.Tables[0]); } #endregion //设置数据源 public void SetSourceData(object sourceData) { dt_sourceData = (DataTable)sourceData; //////获取总项数 ZongXiangShu = dt_sourceData.Rows.Count; //////总页数 ZongYeShu = (ZongXiangShu / m_itemsPerPage) + ((ZongXiangShu % m_itemsPerPage != 0) ? 1 : 0);//总页数 UpdateUIData(PageState.Init); } //更新界面信息 private void UpdateUIData(PageState pageState) { SetDGVSourceData(pageState);//初始化数据 m_currentPage += (int)pageState; UpdateButtonState(); //更新按钮状态 SetCurrOfTotalPage();//设置[当前页/总页码] } //设置DGV的数据源<显示数据> private void SetDGVSourceData(PageState pageState) { DataTable viewData = dt_sourceData.Clone();//复制组织架构 int nBeginIndex = 0; int nEndIndex = 0; GetCurrentPageIndex(pageState, out nBeginIndex, out nEndIndex); for (; nBeginIndex < nEndIndex; nBeginIndex++) { viewData.ImportRow(dt_sourceData.Rows[nBeginIndex]); } dataGridView1.DataSource = viewData; } //设置当前页码/总页码 private void SetCurrOfTotalPage() { label1.Text = m_currentPage.ToString() + " / " + ZongYeShu.ToString() + " 页"; } //获取当前页码项的最小下标和最大下标 private void GetCurrentPageIndex(PageState pageState, out int beginIndex, out int endIndex) { if (PageState.Init == pageState)//初始化 { beginIndex = 0; endIndex = m_itemsPerPage; } else if (PageState.Pre == pageState)//上一页 { endIndex = m_currentPage * m_itemsPerPage; beginIndex = (m_currentPage - 1) * m_itemsPerPage; } else//下一页 { beginIndex = m_currentPage * m_itemsPerPage; endIndex = (m_currentPage + 1) * m_itemsPerPage; } //处理越界情况 if (beginIndex < 0) { beginIndex = 0; } if (endIndex > ZongXiangShu) { endIndex = ZongXiangShu; } } //上一页按钮 private void TSB_PrePage_Click(object sender, EventArgs e) { UpdateUIData(PageState.Pre); } //下一页按钮 private void TSB_NextPage_Click(object sender, EventArgs e) { UpdateUIData(PageState.Next); } //更新按钮状态 private void UpdateButtonState() { //处理上一页按钮 if (1 == m_currentPage) { if (TSB_PrePage.Enabled)//屏蔽 { TSB_PrePage.Enabled = false; } } else if (!TSB_PrePage.Enabled) { TSB_PrePage.Enabled = true; } //处理下一页按钮 if (ZongYeShu == m_currentPage) { if (TSB_NextPage.Enabled) { TSB_NextPage.Enabled = false; } } else if (!TSB_NextPage.Enabled) { TSB_NextPage.Enabled = true; } } } }
时间: 2024-10-20 16:04:04