分页

来源:互联网 发布:js购物车选中删除 编辑:程序博客网 时间:2024/05/16 19:09

public partial class 存储过程分页 : Form
    {
        int pagesize = 4;
        int pageindex = 1;
        int recordcount = 0;
        int totalsize = 0;
        public 存储过程分页()
        {
            InitializeComponent();
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            pageindex = 1;
            DataBinds(pageindex);
        }

        private void btnPre_Click(object sender, EventArgs e)
        {
            if (pageindex > 1)
            {
                pageindex--;
            }
           
            DataBinds(pageindex);
        }

        private void btnNext_Click(object sender, EventArgs e)
        {

            if (pageindex < totalsize)
            {
                pageindex++;
            }           
            DataBinds(pageindex);
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            DataBinds(totalsize);
        }

        private void 存储过程分页_Load(object sender, EventArgs e)
        {
            recordcount= GetCount();
            if (recordcount % pagesize != 0)
            {
                totalsize = recordcount / pagesize + 1;
            }
            else
            {
                totalsize = recordcount / pagesize;
            }
            DataBinds(pageindex);
        }
        private void DataBinds(int pageindex)
        {
            SqlConnection conn = new SqlConnection(@"Data Source=YHB-PC\SQLSERVER2008;Initial Catalog=MyFirst;Persist Security Info=True;User ID=sa;Password=yhb@163");
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            conn.Open();
            cmd.CommandText = "profenye";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@pageindex",pageindex);
            cmd.Parameters.AddWithValue("@pagesize", pagesize);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            this.dataGridView1.DataSource = dt;
            conn.Close();
            conn.Dispose();
        }

        //获取记录数量
        private int GetCount()
        {
            SqlConnection conn = new SqlConnection(@"Data Source=YHB-PC\SQLSERVER2008;Initial Catalog=MyFirst;Persist Security Info=True;User ID=sa;Password=yhb@163");
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            conn.Open();
            cmd.CommandText = "select count(*) from T_UserInfo";
            int count=Convert.ToInt32(cmd.ExecuteScalar());
            cmd.Dispose();
            conn.Close();
            conn.Dispose();
            return count;
        }
    }

原创粉丝点击