Simple implementation of winform paging case~

Hello everyone, I’m Xiongxiong, welcome to follow WeChat public account: Xiongxiong’s small class.

Recently, the fourth class is working on the KTV song ordering system, and it happens to use the content of the paging, so today I will organize it and write a simple winfrom paging case. The following is a screenshot of the case:

Case study: When the form is loaded, the content of the first page is displayed by default. When the "Next page" button is clicked, the content above will be replaced with the content of the second page. Click the "Previous page" button, and the The displayed content will be replaced with the content of the first page, and all displayed content will be queried and displayed from the database.

The current page and total number of pages displayed in the middle of the previous page and next page buttons will change when the page is turned.

Let's talk about the realization idea (collection paging):

  1. Clear all label controls.
  2. Query all data from the database and place it in the collection.
  3. Loop through the data in the collection and display it in the label control.
  4. Paging display is realized by the formula (pageIndex-1)*pageSize, where pageIndex is the current page and pageSize is the page size.

How to calculate the total number of pages:

Get the length of the data in the collection, that is, the total number of records, and the page size (the number of items displayed on each page) to calculate the total number of pages:

The total number is divided by the page size. If the remainder is 0, the quotient is the total number of pages, otherwise, 1 is added to the quotient to obtain the total number of pages.

Here is the key code:

1. Declared public variables:

/所有的数据        List<string> gList = new List<string>();        //当前页        int pageIndex = 1;        //总页数        int totalPage = 0;        //页大小        int pageSize = 4;          DBHelper db = new DBHelper();

2. The loading event of the form:

private void frmPage_Load(object sender, EventArgs e)        {            //清空控件内容            ClearLabel();            //查询所有数据            FindGradeAll();            //总页数等于集合中的所有数据与页大小之间的计算            totalPage = gList.Count % pageSize == 0 ? gList.Count / pageSize : gList.Count / pageSize + 1;            //总页数            this.lblTotalPage.Text = totalPage.ToString();            //当前页            this.lblPage.Text = pageIndex.ToString();            //分页查询,默认显示第一页的内容            PageShowGrade(1);         }

3. Query all grade names and put them in the label

//查询所有的年级名称放在集合中        public void FindGradeAll() {            string sql = "select * from grade";            SqlDataReader dr = db.ExecuteReader(sql);             while (dr.Read())            {                gList.Add(dr["gname"].ToString());            }            dr.Close();                }

4. Clear all content in Label

//清空Label中的所有内容        public void ClearLabel() {             //获取panel中有多少个控件            int count = this.panel1.Controls.Count;            //清空            for (int i = 0; i < count;i++ ) {                Label lbl = (Label)this.panel1.Controls[i];                lbl.Text = "暂无数据";            }        }

5. Paging display data

//分页显示数据        public void PageShowGrade(int pageIndex) {                        for (int i = 0; i < this.panel1.Controls.Count;i++ ) {                Label lbl = (Label)this.panel1.Controls[i];                int v = (pageIndex - 1) * pageSize + i;                lbl.Text = gList[v];            }        }

6. Next page button

//下一页        private void button2_Click(object sender, EventArgs e)        {            //当前是最后一页            if (pageIndex == totalPage)            {                MessageBox.Show("现在已经是最后一页了");            }            else {                //当前页加1                pageIndex++;                //分页查询                PageShowGrade(pageIndex);                //更新当前页                this.lblPage.Text = pageIndex.ToString();            }                   }

7. Previous page button;

//上一页        private void button1_Click(object sender, EventArgs e)        {                       if (pageIndex <=1 )            {                //默认为第一页                pageIndex = 1;                MessageBox.Show("现在已经是第一页了");            }            else            {                //当前页减1                pageIndex--;                //分页查询                PageShowGrade(pageIndex);                //更新当前也                this.lblPage.Text = pageIndex.ToString();            }        }

The above are all examples. Have you learned it?