孤独的猫

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

WinForm 里面的DataGridView不像WebForm里面的GridView那样有自带的分页功能,需要自己写代码来实现分页,效果如下图: 分页控件 

 

.CS:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 using System.Data.SqlClient;
 10 
 11 namespace allenPageTest
 12 {
 13     public partial class Form2 : Form
 14     {
 15         public Form2()
 16         {
 17             InitializeComponent();
 18         }
 19 
 20         private void Form2_Load(object sender, EventArgs e)
 21         {
 22             BindDataWithPage(1);
 23         }
 24 
 25         //总记录数
 26         public int RecordCount = 0;
 27         private string strConn = @"server=.;database=test;uid=sa;pwd=1234";         
 28         private string strProcedure = "PageTest ";
 29 
 30 
 31         /// <summary>
 32         /// 绑定第Index页的数据
 33         /// </summary>
 34         /// <param name="Index"></param>
 35         private void BindDataWithPage(int Index)
 36         {
 37             allenPage1.PageIndex = Index;
 38             //winFormPager1.PageSize = 10;
 39             DataTable dt = GetData(strConn, strProcedure, Index, allenPage1.PageSize);
 40 
 41             dataGridView1.DataSource = dt;
 42 
 43             //获取并设置总记录数
 44             allenPage1.RecordCount = RecordCount;
 45         }
 46 
 47 
 48         /// <summary>
 49         /// 获取数据源
 50         /// </summary>
 51         /// <param name="conn">连接对象</param>
 52         /// <param name="strProcedure">存储过程名称</param>
 53         /// <param name="pageIndex">页码</param>
 54         /// <param name="pageSize">每一页显示的行数</param>
 55         /// <returns></returns>
 56         private DataTable GetData(string conn, string strProcedure, int pageIndex, int pageSize)
 57         {
 58 
 59             using (SqlConnection connection = new SqlConnection(conn))
 60             {
 61                 SqlCommand command = new SqlCommand(strProcedure, connection);
 62                 command.CommandType = CommandType.StoredProcedure;//采用存储过程                 
 63                 command.Parameters.Add("@Table", SqlDbType.NVarChar, 1000).Value = "TableName";//对应的数据表名
 64                 command.Parameters.Add("@TIndex", SqlDbType.NVarChar, 100).Value = "Index";//主键ID
 65                 command.Parameters.Add("@Column", SqlDbType.NVarChar, 2000).Value = "*";//要查询的字段,*为全部字段
 66                 command.Parameters.Add("@Sql", SqlDbType.NVarChar, 3000).Value = " 1=1 ";//查询条件
 67                 command.Parameters.Add("@PageIndex", SqlDbType.Int, 8).Value = pageIndex.ToString();//当前页码
 68                 command.Parameters.Add("@PageSize", SqlDbType.Int, 8).Value = pageSize.ToString();//每一页显示的行数
 69                 command.Parameters.Add("@Sort", SqlDbType.NVarChar, 200).Value = " Column Name asc";//排序的字段
 70                 //打开连接
 71                 if (connection.State != ConnectionState.Open) 
 72                 { 
 73                     connection.Open(); 
 74                 }
 75                 try
 76                 {
 77                     //填充数据
 78                     SqlDataAdapter da = new SqlDataAdapter(command);
 79                     DataSet ds = new DataSet();
 80                     da.Fill(ds);
 81                     //获取总记录数
 82                     RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
 83                     //返回数据集
 84                     return ds.Tables[0];
 85 
 86                 }
 87                 catch (SqlException err)
 88                 {
 89                     MessageBox.Show(err.Message);
 90                     return null; ;
 91                 }
 92                 finally
 93                 {
 94                     connection.Close();
 95                 }
 96             }
 97         }
 98 
 99         private void allenPage1_PageIndexChanged(object sender, EventArgs e)
100         {
101             BindDataWithPage(allenPage1.PageIndex);
102         }
103     }
104 }

 

posted on 2014-10-24 11:28  孤独的猫  阅读(0)  评论(0编辑  收藏  举报