C#编程-79:DataGridView分页显示_C#编程
 
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Windows.Forms;
  5.  
  6. namespace DataGridViewDataNavigate
  7. {
  8.     public partial class Form1 : Form
  9.     {
  10.         public Form1()
  11.         {
  12.             InitializeComponent();
  13.         }
  14.         SqlDataAdapter sqladp;
  15.         DataSet dateSet = new DataSet();
  16.         private int startVal=0;//起始值
  17.         private int perPageVal = 3;//每页显示数量
  18.         private int totalVal;//总条数
  19.         private int currentPage=0;//当前页
  20.         private void Form1_Load(object sender, EventArgs e)
  21.         {
  22.             string constr = @"server=(localdb)\Projects;integrated security=sspi;database=company";
  23.             SqlConnection sqlcon = new SqlConnection(constr);
  24.             try
  25.             {
  26.                 sqlcon.Open();
  27.                 string sql = "select * from clerk";
  28.                 sqladp = new SqlDataAdapter(sql,sqlcon);
  29.                 sqladp.Fill(dateSet,"clerk");
  30.                 //this.dataGridView1.DataSource = dateSet.Tables["clerk"];
  31.                 sqlcon.Close();
  32.  
  33.                 //计算总页数
  34.                 totalVal = dateSet.Tables["clerk"].Rows.Count;
  35.                 int totalPage=(totalVal%perPageVal==0)?(totalVal/perPageVal):(totalVal/perPageVal+1);
  36.                 lblTotalPage.Text = "/" + totalPage;
  37.                 LoadData();
  38.             }
  39.             catch (Exception ex)
  40.             {
  41.  
  42.                 MessageBox.Show(ex.Message);
  43.             }
  44.  
  45.         }
  46.         private void LoadData()
  47.         {
  48.             //显示当前页面
  49.             currentPage = startVal / perPageVal + 1;
  50.             txtCurrentPage.Text = currentPage.ToString();
  51.  
  52.             //绑定数据
  53.             dateSet.Clear();
  54.             sqladp.Fill(dateSet, startVal, perPageVal, "clerk");
  55.              
  56.            bindingSource1.DataSource=dateSet.Tables[0];
  57.            bindingNavigator1.BindingSource = bindingSource1;
  58.            dataGridView1.DataSource = bindingSource1;
  59.         }
  60.  
  61.         private void bindingNavigator1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
  62.         {
  63.             if (e.ClickedItem.Text == "上一页")
  64.             {
  65.                 startVal = startVal - perPageVal;
  66.                 if (startVal < 0)
  67.                 {
  68.                     startVal = 0;
  69.                     MessageBox.Show("已经是第一页");
  70.                     return;
  71.                 }
  72.             }
  73.             if (e.ClickedItem.Text == "下一页")
  74.             {
  75.                 startVal = startVal + perPageVal;
  76.                 if (startVal > totalVal)
  77.                 {
  78.                     startVal = startVal - perPageVal;
  79.                     MessageBox.Show("已经是最后一页");
  80.                     return;
  81.                 }
  82.              
  83.             }
  84.             LoadData();
  85.         }
  86.     }
  87. }