使用BindingNavigator,在DataGridView中分页数据

博客介绍了BindingNavigator相关代码,多数在Designer中,移动行操作无需自行编写。还提到DataTable用于存放数据库数据,需先从数据库取数。上一页和下一页需自行编程实现,要在BindingNavigator上定义事件处理方法。此外,介绍了初始化页面结构、回车跳转指定页等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

其中上一行,下一行,首行,尾行 无需我们操心。

关于BindingNavigator的代码,大部分都在Designer中,无需我们自己编写。包括移动到上一行,下一行,首行,尾行都不用我们自己写代码实现。

关于BindingNavigator(bdnInfo)

./Form3.cs:            bdnInfo.BindingSource = bdsInfo;
./Form3.cs:        private void bdnInfo_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
./Form3.Designer.cs:            this.bdnInfo = new System.Windows.Forms.BindingNavigator(this.components);
./Form3.Designer.cs:            ((System.ComponentModel.ISupportInitialize)(this.bdnInfo)).BeginInit();
./Form3.Designer.cs:            this.bdnInfo.SuspendLayout();
./Form3.Designer.cs:            this.panel1.Controls.Add(this.bdnInfo);
./Form3.Designer.cs:            // bdnInfo
./Form3.Designer.cs:            this.bdnInfo.AddNewItem = null;
./Form3.Designer.cs:            this.bdnInfo.CountItem = this.lblTotalLine;
./Form3.Designer.cs:            this.bdnInfo.DeleteItem = null;
./Form3.Designer.cs:            this.bdnInfo.Dock = System.Windows.Forms.DockStyle.Fill;
./Form3.Designer.cs:            this.bdnInfo.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
./Form3.Designer.cs:            this.bdnInfo.Location = new System.Drawing.Point(0, 0);
./Form3.Designer.cs:            this.bdnInfo.MoveFirstItem = this.bindingNavigatorMoveFirstItem1;
./Form3.Designer.cs:            this.bdnInfo.MoveLastItem = this.bindingNavigatorMoveLastItem1;
./Form3.Designer.cs:            this.bdnInfo.MoveNextItem = this.bindingNavigatorMoveNextItem1;
./Form3.Designer.cs:            this.bdnInfo.MovePreviousItem = this.bindingNavigatorMovePreviousItem1;
./Form3.Designer.cs:            this.bdnInfo.Name = "bdnInfo";
./Form3.Designer.cs:            this.bdnInfo.PositionItem = this.txtCurrentLine;
./Form3.Designer.cs:            this.bdnInfo.Size = new System.Drawing.Size(623, 49);
./Form3.Designer.cs:            this.bdnInfo.TabIndex = 0;
./Form3.Designer.cs:            this.bdnInfo.Text = "bindingNavigator1";
./Form3.Designer.cs:            this.bdnInfo.ItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.bdnInfo_ItemClicked);
./Form3.Designer.cs:            ((System.ComponentModel.ISupportInitialize)(this.bdnInfo)).EndInit();
./Form3.Designer.cs:            this.bdnInfo.ResumeLayout(false);
./Form3.Designer.cs:            this.bdnInfo.PerformLayout();
./Form3.Designer.cs:        private System.Windows.Forms.BindingNavigator bdnInfo;

关于BindingSource(bdsInfo)

./Form3.cs:            bdsInfo.DataSource = dtTemp;
./Form3.cs:            bdnInfo.BindingSource = bdsInfo;
./Form3.cs:            dgvInfo.DataSource = bdsInfo;
./Form3.Designer.cs:            this.bdsInfo = new System.Windows.Forms.BindingSource(this.components);
./Form3.Designer.cs:            ((System.ComponentModel.ISupportInitialize)(this.bdsInfo)).BeginInit();
./Form3.Designer.cs:            ((System.ComponentModel.ISupportInitialize)(this.bdsInfo)).EndInit();
./Form3.Designer.cs:        private System.Windows.Forms.BindingSource bdsInfo;

关于DataTable(dtTemp)

./Form3.cs:            DataTable dtTemp = dtInfo.Clone();   //克隆DataTable结构框架
./Form3.cs:                dtTemp.ImportRow(dtInfo.Rows[i]);
./Form3.cs:            bdsInfo.DataSource = dtTemp;

关于DataTable(dtInfo)

./Form3.cs:        private DataTable dtInfo = new DataTable();
./Form3.cs:            nMax = dtInfo.Rows.Count;
./Form3.cs:            DataTable dtTemp = dtInfo.Clone();   //克隆DataTable结构框架
./Form3.cs:                dtTemp.ImportRow(dtInfo.Rows[i]);
./Form3.cs:            this.dtInfo = SQLiteHelper.ExecuteQuery("select * from WXMessage", null);

dtInfo用于存放数据库里面的数据,所以需要首先将数据从数据库里面取出来。

 private void setDataTable()
        {
            string cd = System.Environment.CurrentDirectory;
            string pd = Directory.GetParent(Directory.GetParent(cd).FullName).FullName;
            pd += "\\midtrans.db";
            SQLiteHelper.SetConnectionString(pd, null, 3);
            this.dtInfo = SQLiteHelper.ExecuteQuery("select * from WXMessage", null);
        }

关于上一页和下一页的实现,需要自己编写程序。就连上一页和下一页这样的控件(这里使用的是ToolStripLabel),也需要自己从工具箱里面拖拽上去,放入BindingNavigator内部。因为存放在了大控件的内部,所以需要在大控件(BindingNavigator)上定义处理该事件的方法:

即ItemClicked事件。

 private void bdnInfo_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Text == "关闭")
            {
                this.Close();
            }
            if (e.ClickedItem.Text == "上一页")
            {
                //pageCurrent--;
                if (pageCurrent <= 1)
                {
                    MessageBox.Show("已经是第一页,请点击“下一页”查看!");
                    return;
                }
                else
                {
                    pageCurrent--;
                    nCurrent = pageSize * (pageCurrent - 1);
                }
                LoadData();
            }
            if (e.ClickedItem.Text == "下一页")
            {

                if (pageCurrent >= pageCount)
                {
                    MessageBox.Show("已经是最后一页,请点击“上一页”查看!");
                    return;
                }
                else
                {
                    pageCurrent++;
                    nCurrent = pageSize * (pageCurrent - 1);
                }
                LoadData();
            }
        }

初始化页面结构

private int pageSize = 0;     //每页显示行数
        private int nCurrent = 0;      //当前记录行
        private int pageCount = 0;    //总共有多少页=总记录数/每页显示行数
        private int pageCurrent = 0;   //当前页号
        private int nMax = 0;         //总记录数

        private DataTable dtInfo = new DataTable();

        private void InitDataSet()
        {
            setDataTable();//----------------------
            pageSize = 5;      //设置页面行数
            nMax = dtInfo.Rows.Count;

            pageCount = (nMax / pageSize);    //计算出总页数

            if ((nMax % pageSize) > 0) pageCount++;

            pageCurrent = 1;    //当前页数从1开始
            nCurrent = 0;       //当前记录数从0开始

            LoadData();
        }

使用LoadData初始化指定页的数据,并进行将DataGridView中的数据更新之后展示

LoadData根据CurrentPage计算出来nStartPos和nEndPos这两个关键数据,利用这两个数据将数据从已经绑定了DataSource的DataTable中取出制定的数据,也就是说,可以利用这连个变量分段截取DataTable中的数据,然后展示在DataGridView中。

 private void LoadData()
        {
            int nStartPos = 0;   //当前页面开始记录行
            int nEndPos = 0;     //当前页面结束记录行

            DataTable dtTemp = dtInfo.Clone();   //克隆DataTable结构框架

            if (pageCurrent == pageCount)
                nEndPos = nMax;
            else
                nEndPos = pageSize * pageCurrent;//(按照下标从0开始算起,那么这应该指向的是下一页的第一条记录对应的下标)

            nStartPos = nCurrent;

            lblPageCount.Text = "/" + pageCount.ToString();
            txtCurrentPage.Text = Convert.ToString(pageCurrent);
            //从元数据源复制记录行
            for (int i = nStartPos; i < nEndPos; i++)
            {
                dtTemp.ImportRow(dtInfo.Rows[i]);
                nCurrent++;
            }
            bdsInfo.DataSource = dtTemp;
            bdnInfo.BindingSource = bdsInfo;
            dgvInfo.DataSource = bdsInfo;
        }

回车之后跳转到指定页

这里为这个控件绑定KeyPress事件

并且使用正则表达式,只让数字通过

 private void enter(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13) return;
            string pattern = @"^\d+$";
            string input = this.txtCurrentPage.Text;
            Regex regex = new Regex(pattern);
            //判断是否匹配成功
            bool res = regex.IsMatch(input);
            if (!res) return;
            pageCurrent = int.Parse(this.txtCurrentPage.Text);

            if (pageCurrent > pageCount || pageCurrent == 0) return;
            nCurrent = pageSize * (pageCurrent - 1);
            LoadData();
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值