c#,用户控件,分页浏览,winform,datagridview,BandingNavigator,bandingSource

本文介绍了一个分页用户控件的实现方法,包括如何设置数据源、计算总页数和填充数据等关键步骤。该控件可用于查询窗体中进行高效的数据分页展示。
SelectPaging.JPG

分页用户控件,使用效果如上图,可以用在查询窗体,代码如下:

None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.ComponentModel;
None.gif
using System.Drawing;
None.gif
using System.Data;
None.gif
using System.Text;
None.gif
using System.Windows.Forms;
None.gif
None.gif
namespace Kimbanx.SecurityDiskSystem.Controls
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public partial class SelectPaging : UserControl
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{/**//// <summary>
InBlock.gif        
/// 每页显示记录数
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private int _pageSize = 20;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 每页显示记录数
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public int PageSize
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _pageSize; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _pageSize = value; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
/**//// <summary>
InBlock.gif        
/// 总记录数
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private int _nMax = 0;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 总记录数
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public int NMax
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _nMax; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _nMax = value; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
/**//// <summary>
InBlock.gif        
/// 页数=总记录数/每页显示记录数
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private int _pageCount = 0;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 页数=总记录数/每页显示记录数
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public int PageCount
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _pageCount; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _pageCount = value; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
/**//// <summary>
InBlock.gif        
/// 当前页号
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private int _pageCurrent = 0;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 当前页号
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public int PageCurrent
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _pageCurrent; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _pageCurrent = value; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
/**//// <summary>
InBlock.gif        
/// 当前记录行
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private int _nCurrent = 0;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 当前记录行
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public int NCurrent
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _nCurrent; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _nCurrent = value; }
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设置数据源
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private DataTable dataSource = null;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设置数据源
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public DataTable DataSource
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn dataSource; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ dataSource = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//private DataSet ds = null;
InBlock.gif

InBlock.gif        
public void InitDataSet(DataTable dt)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.dataSource = dt;
InBlock.gif            
this._nMax = this.dataSource.Rows.Count;
InBlock.gif            
//计算总页数
InBlock.gif
            this._pageCount = _nMax / _pageSize;
InBlock.gif            
//
InBlock.gif
            if ((_nMax % _pageSize) > 0)
InBlock.gif                _pageCount
++;
InBlock.gif            _pageCurrent 
= 1;
InBlock.gif            _nCurrent 
= 0;
InBlock.gif            FillData();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 默认构造函数
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public SelectPaging()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();
InBlock.gif            lblMsg.Enabled 
= false;
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 填充数据
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void FillData()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//当前页面开始记录行
InBlock.gif
            int nStartPos = 0;
InBlock.gif            
//当前页面结束记录行
InBlock.gif
            int nEndPos = 0;
InBlock.gif            DataTable dtTemp 
= this.dataSource.Clone();
InBlock.gif            
if (this._pageCurrent == this._pageCount)
InBlock.gif                nEndPos 
= _nMax;
InBlock.gif            
else
InBlock.gif                nEndPos 
= this._pageCurrent * this._pageSize;
InBlock.gif
InBlock.gif            nStartPos 
= this._nCurrent;
InBlock.gif
InBlock.gif            lblMaxPage.Text 
= this._pageCount.ToString();
InBlock.gif            txtCurrentPage.Text 
= this._pageCurrent.ToString();
InBlock.gif
InBlock.gif            
for (int i = nStartPos; i < nEndPos; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                dtTemp.ImportRow(
this.dataSource.Rows[i]);
InBlock.gif                _nCurrent
++;
ExpandedSubBlockEnd.gif            }

InBlock.gif            bdSource.DataSource 
= dtTemp;
InBlock.gif            bdNav.BindingSource 
= bdSource;
InBlock.gif            dataGV.DataSource 
= bdSource;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void bdNav_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
switch (e.ClickedItem.Text)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
case "移到上一条记录":
InBlock.gif                    
//FillData();
InBlock.gif
                    break;
InBlock.gif                
case "移到下一条记录":
InBlock.gif                    
//FillData();
InBlock.gif
                    break;
InBlock.gif                
case "上一页":
InBlock.gif                   
InBlock.gif                    
if (this._pageCurrent == 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        lblMsg.Text 
= "已经是第一页,请点击“下一页”查看!";
InBlock.gif                        
//btnPrePage.Enabled = false;
InBlock.gif
                        return;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{ _pageCurrent--;
InBlock.gif                        
this._nCurrent = this._pageSize * (_pageCurrent - 1);
InBlock.gifFillData();
ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    
break;
InBlock.gif                
case "下一页":
InBlock.gif                    
InBlock.gif                    
if (this._pageCurrent == _pageCount)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        lblMsg.Text 
= "已经是最后一页,请点击“上一页”查看!";
InBlock.gif                        
return;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif_pageCurrent
++;
InBlock.gif                        
this._nCurrent = this._pageSize * (_pageCurrent - 1); FillData();
ExpandedSubBlockEnd.gif                    }

InBlock.gif                   
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值