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

SelectPaging.JPG

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

using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Drawing;
using  System.Data;
using  System.Text;
using  System.Windows.Forms;

namespace  Kimbanx.SecurityDiskSystem.Controls
{
    
public partial class SelectPaging : UserControl
    
{/// <summary>
        
/// 每页显示记录数
        
/// </summary>

        private int _pageSize = 20;
        
/// <summary>
        
/// 每页显示记录数
        
/// </summary>

        public int PageSize
        
{
            
get return _pageSize; }
            
set { _pageSize = value; }
        }
/// <summary>
        
/// 总记录数
        
/// </summary>

        private int _nMax = 0;
        
/// <summary>
        
/// 总记录数
        
/// </summary>

        public int NMax
        
{
            
get return _nMax; }
            
set { _nMax = value; }
        }
/// <summary>
        
/// 页数=总记录数/每页显示记录数
        
/// </summary>

        private int _pageCount = 0;
        
/// <summary>
        
/// 页数=总记录数/每页显示记录数
        
/// </summary>

        public int PageCount
        
{
            
get return _pageCount; }
            
set { _pageCount = value; }
        }
/// <summary>
        
/// 当前页号
        
/// </summary>

        private int _pageCurrent = 0;
        
/// <summary>
        
/// 当前页号
        
/// </summary>

        public int PageCurrent
        
{
            
get return _pageCurrent; }
            
set { _pageCurrent = value; }
        }
/// <summary>
        
/// 当前记录行
        
/// </summary>

        private int _nCurrent = 0;
        
/// <summary>
        
/// 当前记录行
        
/// </summary>

        public int NCurrent
        
{
            
get return _nCurrent; }
            
set { _nCurrent = value; }
        }

        
/// <summary>
        
/// 设置数据源
        
/// </summary>

        private DataTable dataSource = null;
        
/// <summary>
        
/// 设置数据源
        
/// </summary>

        public DataTable DataSource
        
{
            
get return dataSource; }
            
set { dataSource = value; }
        }

        
//private DataSet ds = null;

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

        
/// <summary>
        
/// 默认构造函数
        
/// </summary>

        public SelectPaging()
        
{
            InitializeComponent();
            lblMsg.Enabled 
= false;

        }

        
/// <summary>
        
/// 填充数据
        
/// </summary>

        private void FillData()
        
{
            
//当前页面开始记录行
            int nStartPos = 0;
            
//当前页面结束记录行
            int nEndPos = 0;
            DataTable dtTemp 
= this.dataSource.Clone();
            
if (this._pageCurrent == this._pageCount)
                nEndPos 
= _nMax;
            
else
                nEndPos 
= this._pageCurrent * this._pageSize;

            nStartPos 
= this._nCurrent;

            lblMaxPage.Text 
= this._pageCount.ToString();
            txtCurrentPage.Text 
= this._pageCurrent.ToString();

            
for (int i = nStartPos; i < nEndPos; i++)
            
{
                dtTemp.ImportRow(
this.dataSource.Rows[i]);
                _nCurrent
++;
            }

            bdSource.DataSource 
= dtTemp;
            bdNav.BindingSource 
= bdSource;
            dataGV.DataSource 
= bdSource;

        }


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

                    
else
                    
{ _pageCurrent--;
                        
this._nCurrent = this._pageSize * (_pageCurrent - 1);
FillData();
                    }


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

                    
else
                    
{
_pageCurrent
++;
                        
this._nCurrent = this._pageSize * (_pageCurrent - 1); FillData();
                    }

                   
                    
break;
            }

            
        }

    }

}



你可能感兴趣的:(datagridview)