包括连接数据库,能实现分页,增加记录、删除记录、查询等功能。分不够可以加。
但一定是实际调试过的。

解决方案 »

  1.   

    连接代码public List<Pro> Search(int num1,int num2)
            {
                using (conn = new SqlConnection(DBHelper.connstring))
                {
                    conn.Open();
                    List<Pro> list = new List<Pro>();
                    sql = "select top "+num1+" * from pro except select top "+num2+" * from pro";
                    command = new SqlCommand(sql, conn);
                    reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        Pro pro = new Pro();
                        pro.ProId = (int)reader[0];
                        pro.ProName = reader[1].ToString();
                        pro.ProPrice = float.Parse(reader[2].ToString());
                        pro.ProNum = (int)reader[3];
                        pro.ProType = reader[4].ToString();
                        list.Add(pro);
                    }
                    return list;
                }
            }        public int SearchNum()
            { 
               using(conn=new SqlConnection(DBHelper.connstring))
               {
                   conn.Open();
                   sql = "select count(*) from pro";
                   command = new SqlCommand(sql,conn);
                   int count = (int)command.ExecuteScalar();               return count;
               }
            }实现代码:
     private int count;//数据库中有多少条数据
            private int a = 1;//第几页
            private int b = 8;//每页要显示多少条数据
            private void Form1_Load(object sender, EventArgs e)
            {
                count = new ProManage().SearchNum();
                a = 1;
                int num1 = b;
                int num2 = 0;
                List<Pro> list = new ProManage().Search(num1, num2);
                this.dataGridView1.DataSource = list;
            }        private void btnOne_Click(object sender, EventArgs e)
            {
                //第一页
                a = 1;
                int num1 = b;
                int num2 = 0;
                List<Pro> list = new ProManage().Search(num1, num2);
                this.dataGridView1.DataSource = list;
            }        private void btnUp_Click(object sender, EventArgs e)
            {
                //上一页
                int num1 = b * a;
                int num2 = a * b - b;
                if (num2 < b)
                {
                    MessageBox.Show("已是第一页了");
                    return;
                }
                a--;
                num1 = b * a;
                num2 = b * a - b;
                List<Pro> list = new ProManage().Search(num1, num2);
                this.dataGridView1.DataSource = list;
            }        private void btnTwo_Click(object sender, EventArgs e)
            {
                //最后一页
                int aaaa = count;
                int num1 = 0;
                int num2 = 0;
                if (aaaa % b != 0)//判断总数据与要显示的数据是不是整除;
                {
                    while (true)
                    {
                        if (aaaa <= b) //如果aaaa小于b那么就结束,否则aaaa=0;
                        {
                            break;
                        }else if (aaaa > b)
                        {
                            aaaa = aaaa % b;
                        }                
                        
                    }
                     num1 = count;
                     num2 = count - aaaa;//如果不整除,那么最后一行,肯定不足要显示的数据;
                }
                else
                {
                     num1 = count;
                     num2 = count - b;//如要整除,最后一行,肯定刚好;
                }
                List<Pro> list = new ProManage().Search(num1, num2);
                this.dataGridView1.DataSource = list;
                if (count % b != 0)//如果不整除,最后一行不足,要多增加一页;
                {
                    a = ((count / b)+1);
                }
                else
                {
                    a = ((count / b));//如果整除,最后一行刚好,不需增加;
                }        }        private void btnDown_Click(object sender, EventArgs e)
            {
                //下一页
                int num1 = b * a;
                int num2 = b * a - b;
                if (count <= num1)
                {
                    MessageBox.Show("已是最后一页");
                    return;
                }
                a++;
                num1 = b * a;
                num2 = b * a - b;
                List<Pro> list = new ProManage().Search(num1, num2);
                this.dataGridView1.DataSource = list;
            }这个是分页代码增删改  那也比较简单  具体代码我就不贴了
      

  2.   

    DataGridView分页:http://www.cnblogs.com/sayu115/archive/2007/10/12/922115.html
      

  3.   

    zhanghang19890621 能把所有完整的代码都贴出来吗?80分先给你
      

  4.   

    网上的代码很多的.如:
    http://download.csdn.net/source/889347
      

  5.   

    还有1楼,你的List <Pro>是什么意思?PRO没有定义呀!
    list泛型后跟的是一个类似string的东东吧?