Using bindingNavigator and bindingSource to implement paging in Winform

BindingNavigator control introduction

You can use the BindingNavigator control to create standardized methods for users to search and change data on Windows Forms. The BindingNavigator control consists of a ToolStrip that contains a series of ToolStripItem objects, which can implement: add data, delete data, display points and so on. Combined with a BindingSource, users can move between and interact with data records on a form.

BindingSource control introduction

The BindingSource control is one of the new controls provided by the .NET Framework 2.0. The BindingSource control establishes a connection with the data source, and then establishes a binding relationship between the controls in the form and the BindingSource control to implement data binding and simplify the data binding process.

BindingNavigator combined with BindingSource

The BindingNavigator control is generally more convenient to use with the BindingSource control, because for each button on the BindingNavigator control, there is a corresponding BindingSource component member, which programmatically allows the same functionality.
For example, to realize how bindingnavigator is bound to datagridview, define a BindingSource, and set the data source of BindingNavigator and DataGridView as BindingSource, which can ensure the synchronization of data between BindingNavigator and DataGridView.

    BindingSource bs = new BindingSource();
    bs.DataSource = dateTabel1;
    bindingNavigator1.BindingSource = bs;
    dataGridView1.DataSource = bs ;

Implement pagination

 //定义分页需要的变量
        int cRows = 0;
        int RowCurrent = 0;
        int cPages = 0;
        int PageCurrent = 0;
        const int PageSize = 7;

        private void LoadData()
        {
            if (PageCurrent == 1)
            {
                ts_PagePrevious.Enabled = false;
            }
            else
            {
                ts_PagePrevious.Enabled = true;
            }
            if (cPages == PageCurrent)
            {
                ts_PageNext.Enabled = false;
            }
            else
            {
                ts_PageNext.Enabled = true;
            }
            ts_PagePostion.Text = PageCurrent.ToString();
            ts_Pages.Text = cPages.ToString();
            int startRow = (PageCurrent - 1) * PageSize;
            int endRow = PageCurrent == cPages ? cRows : PageCurrent * PageSize;
            DataTable dtTemp = table1.Clone();
            if (table1.Rows.Count != 0)
            {
                for (int i = startRow; i < endRow; i++)
                {
                    dtTemp.ImportRow(table1.Rows[i]);
                }
                bindingSource1.DataSource = dtTemp;
                dataGridView1.DataSource = bindingSource1;

            }

        }

        private void bindingNavigator1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Text == "上一页")
            {
                ts_PageNext.Enabled = true;
                if (PageCurrent == 1)
                {
                    ts_PagePrevious.Enabled = false;
                    MessageBox.Show("已经是第一页了");
                    return;
                }
                PageCurrent--;
                LoadData();
            }
            if (e.ClickedItem.Text == "下一页")
            {
                ts_PagePrevious.Enabled = true;
                if (PageCurrent == PageSize)
                {
                    ts_PageNext.Enabled = false;
                    MessageBox.Show("已经是最后一页了");
                    return;
                }
                PageCurrent++;
                LoadData();
            }
        }

        private void bindingNavigatorMoveFirstPage_Click(object sender, EventArgs e)
        {
            PageCurrent = 1;
            bindingNavigatorMoveFirstPage.Enabled = false;
            bindingNavigatorMoveLastPage.Enabled = true;
            ts_PagePrevious.Enabled = false;
            ts_PageNext.Enabled = true;
            LoadData();
        }

        private void bindingNavigatorMoveLastPage_Click(object sender, EventArgs e)
        {
            PageCurrent = cPages;
            bindingNavigatorMoveFirstPage.Enabled = true;
            bindingNavigatorMoveLastPage.Enabled = false;
            ts_PagePrevious.Enabled = true;
            ts_PageNext.Enabled = false;
            LoadData();
        }

        private void ts_PagePostion_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (Convert.ToInt32(ts_PagePostion.Text) > 0 && Convert.ToInt32(ts_PagePostion.Text) <= cPages)
                {
                    PageCurrent = Convert.ToInt32(ts_PagePostion.Text);
                    LoadData();
                }
                else
                {
                    throw new Exception();
                }
            }
            catch (System.Exception ex)
            {
                PageCurrent = 1;
                LoadData();
            }
        }

Then call the LoadData method when displaying the data:

table1 = selectIp.SelectAllIp();

            cRows = table1.Rows.Count;
            cPages = cRows % PageSize == 0 ? cRows / PageSize : cRows / PageSize + 1;
            PageCurrent = 1;
            LoadData();

            dataGridView1.Columns[0].HeaderText = "考场";
            dataGridView1.Columns[1].HeaderText = "ip段";
            dataGridView1.Columns[2].HeaderText = "ip起始段";
            dataGridView1.Columns[3].HeaderText = "ip截至段";

The effect is as follows:

write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325845995&siteId=291194637