Winform中用bindingNavigator和bingdingSource实现分页

BindingNavigator控件介绍

可以使用BindingNavigator控件来创建标准化的方法,以便用户搜索和更改 Windows 窗体上的数据。BindingNavigator 控件由包含一系列 ToolStripItem 对象的ToolStrip组成,可以实现:添加数据,删除数据,分也显示等。结合BindingSource,可以便用户能够在窗体上数据记录之间移动并与记录进行交互。

BindingSource控件介绍

BindingSource控件是.NET Framework 2.0提供的新控件之一。BindingSource控件与数据源建立连接,然后将窗体中的控件与BindingSource控件建立绑定关系来实现数据绑定,简化数据绑定的过程。

BindingNavigator与BindingSource联合使用

BindingNavigator 控件一般要与BindingSource 控件一同使用较为方便,因为对于 BindingNavigator 控件上的每个按钮,都有一个对应的 BindingSource 组件成员,其以编程方式允许有相同功能。
比如要实现bindingnavigator如何与datagridview绑定,定义一个BindingSource ,并将BindingNavigator 和DataGridView的数据源都设置为BindingSource ,可保证BindingNavigator 和DataGridView的数据同步。

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

实现分页

 //定义分页需要的变量
        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();
            }
        }

然后在显示数据的时候调用LoadData方法:

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截至段";

效果如下:

这里写图片描述

猜你喜欢

转载自blog.csdn.net/ldb987/article/details/79824075