//WinForm中DataGridView的分页,需要2个控件,一个BindingSource,一个BindingNavigator。
bnPager为BindingNavigator,BindSource为BindingSource。
int pageSize = 0;
int nMax = 0;
int pageCount = 0;
int pageCurrent = 0;
int nCurrent = 0;
DataSet myDs = new DataSet();
DataTable dt = new DataTable();
public frmPage()
{
InitializeComponent();
FillData();
}
public void FillData() { SqlConnection conn = MyClass.CreateConn(); conn.Open(); SqlDataAdapter myDa = new SqlDataAdapter("select * from bookInfo", conn); myDa.Fill(myDs, "myTable"); conn.Close(); dt = myDs.Tables[0]; InitDataSet(); } private void InitDataSet() { pageSize = 10; nMax = dt.Rows.Count; pageCount = (nMax / pageSize); if ((nMax%pageSize)>0) { pageCount++; } pageCurrent = 1; nCurrent = 0; LoadData(); }
private void LoadData() { int nStartPos = 0; int nEndPos = 0; DataTable dtTemp = dt.Clone(); if (pageCurrent==pageCount) { nEndPos = nMax; } else { nEndPos = pageSize * pageCurrent; } nStartPos = nCurrent; toolStripLabel1.Text = "/ "+pageCount.ToString(); toolStripLabel2.Text = Convert.ToString(pageCurrent); for (int i = nStartPos; i < nEndPos; i++) { dtTemp.ImportRow(dt.Rows[i]); nCurrent++; } BindSource.DataSource=dtTemp; bnPager.BindingSource = BindSource; DGView.DataSource = BindSource; }
private void bnPager_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { if (e.ClickedItem.Text=="上一页") { pageCurrent--; if (pageCurrent<=0) { MessageBox.Show("已经是第一页,请点击“下一页”查看!"); return; } else { nCurrent = pageSize * (pageCurrent - 1); } LoadData(); } if (e.ClickedItem.Text=="下一页") { pageCurrent++; if (pageCurrent>pageCount) { MessageBox.Show("已经是最后一页,请点击“上一页”查看!"); return; } else { nCurrent = pageSize * (pageCurrent - 1); } LoadData(); } }
参考资料: /newsview.asp?id=55
1)首先把GridView的AllowPaging=true。
2)然后在PageIndexChanging事件中e.NewPageIndex获得当前页(从0开始)
Hostio是一家成立于2006年的国外主机商,提供基于KVM架构的VPS主机,AMD EPYC CPU,NVMe硬盘,1-10Gbps带宽,最低月付5欧元起。商家采用自己的网络AS208258,宿主机采用2 x AMD Epyc 7452 32C/64T 2.3Ghz CPU,16*32GB内存,4个Samsung PM983 NVMe SSD,提供IPv4+IPv6。下面列出几款主机配置信息。...
PIGYun是成立于2019年的国人商家,提供香港、韩国和美西CUVIP-9929等机房线路基于KVM架构的VPS主机,本月商家针对韩国首尔、美国洛杉矶CUVIP-AS29、GIA回程带防御等多条线路VPS提供6-8.5折优惠码,优惠后韩国首尔CN2混合BGP特惠型/美国洛杉矶GIA回程带10Gbps攻击防御VPS主机最低每月14.4元起。下面列出几款不同机房VPS主机配置信息,请留意不同优惠码。...
近日CloudCone发布了最新的补货消息,针对此前新年闪购年付便宜VPS云服务器计划方案进行了少量补货,KVM虚拟架构,美国洛杉矶CN2 GT线路,1Gbps带宽,最低3TB流量,仅需14美元/年,有需要国外便宜美国洛杉矶VPS云服务器的朋友可以尝试一下。CloudCone怎么样?CloudCone服务器好不好?CloudCone值不值得购买?CloudCone是一家成立于2017年的美国服务器...