namespace SQL
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int pageIndex = 1;  //页码
        int pageSize = 3;   //每页3条
        int pageCount;      //共几页

        private void Form1_Load(object sender, EventArgs e)
        {
            BindDGV();
        }
     
        void BindDGV()
        {
            DataTable dt = new DataTable();
            using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=myschool;Integrated Security=True"))
            {
                using (SqlCommand cmd = new SqlCommand("usp_GetPaged", conn))
                {
                    //设置执行模式为存储过程
                    cmd.CommandType = CommandType.StoredProcedure;

                    SqlParameter[] param = { 
                                            new SqlParameter("@pageIndex",SqlDbType.Int),
                                            new SqlParameter("@pageSize",SqlDbType.Int),
                                            new SqlParameter("@pageCount",SqlDbType.Int)
                                           };
                    //输入参数
                    param[0].Value = pageIndex;
                    param[1].Value = pageSize;
                    //输出参数
                    param[2].Direction = ParameterDirection.Output;
                    //添加参数
                    cmd.Parameters.AddRange(param);
                    //
                    SqlDataAdapter sda = new SqlDataAdapter(cmd);
                    //执行存储过程
                    sda.Fill(dt);

                    //必须调用完存储过程 才能获取输出参数的值
                    pageCount = Convert.ToInt32(param[2].Value);

                    //设置dgv不自动生成列
                    dataGridView1.AutoGenerateColumns = false;
                    //设置dgv的数据源
                    dataGridView1.DataSource = dt;
                }
            }

            btnFirst.Enabled = true;
            btnLast.Enabled = true;
            btnNext.Enabled = true;
            btnPre.Enabled = true;

            //判断按钮是否可用
            if (pageIndex == 1)
            {
                btnPre.Enabled = false;
                btnFirst.Enabled = false;
            }

            if (pageIndex == pageCount)
            {
                btnLast.Enabled = false;
                btnNext.Enabled = false;
            }
        }

        //首页
        private void btnFirst_Click(object sender, EventArgs e)
        {
            pageIndex = 1;
            BindDGV();
        }
        //尾页
        private void btnLast_Click(object sender, EventArgs e)
        {
            pageIndex = pageCount;
            BindDGV();
        }
        //上一页
        private void btnPre_Click(object sender, EventArgs e)
        {
            if (pageIndex > 1)
            {
                pageIndex--;
                BindDGV();
            }
           
        }
        //下一页
        private void btnNext_Click(object sender, EventArgs e)
        {
            if (pageIndex < pageCount)
            {
                pageIndex++;
                BindDGV();
            }
        }
    }
}
posted on 2012-07-23 22:28  Fan帥帥  阅读(1717)  评论(0编辑  收藏  举报