作者:贾春雨-cherry | 来源:互联网 | 2023-10-11 16:57
其中上一行,下一行,首行,尾行 无需我们操心。
关于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;elsenEndPos = pageSize * pageCurrent;//(按照下标从0开始算起,那么这应该指向的是下一页的第一条记录对应的下标)nStartPos = nCurrent;lblPageCount.Text = "/" + pageCount.ToString();txtCurrentPage.Text = Convert.ToString(pageCurrent);//从元数据源复制记录行for (int i = nStartPos; i 回车之后跳转到指定页
这里为这个控件绑定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();}