datagridview 控件手动代码分页

//注:要进行分页的实体类对象都要实现IPage接口方可使用

public interface IPage
    {
        string[] Export();
    }


    public class PaginManager
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dataGridView">绑定的datagrodview控件</param>
        /// <param name="list">全部的集合</param>
        /// <param name="isShowNum">是否要显示序列号</param>
        /// <param name="isShowId">是否要显示的Id号</param>
        /// <param name="SumMax">每页限制数</param>
        /// <param name="pageSum">总页数</param>
        /// <param name="numMax">总条数</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="numSum">每页的记录数</param>
        public PaginManager(DataGridViewX dataGridView, List<IPage> list, bool isShowNum, bool isShowId, int SumMax, LabelX pageSum, LabelX numMax, TextBoxX pageIndex, TextBoxX numSum)
            : this(dataGridView, list, isShowNum, isShowId, SumMax, pageSum, numMax, pageIndex, numSum, null) { }

        /// <summary>
        /// 还有查询接口的构造函数
        /// </summary>
        /// <param name="dataGridView">绑定的datagrodview控件</param>
        /// <param name="list">全部的集合</param>
        /// <param name="isShowNum">是否要显示序列号</param>
        /// <param name="isShowId">是否要显示的Id号</param>
        /// <param name="SumMax">每页限制数</param>
        /// <param name="pageSum">总页数</param>
        /// <param name="numMax">总条数</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="numSum">每页的记录数</param>
        /// <param name="isearch">查询接口</param>
        public PaginManager(DataGridViewX dataGridView, List<IPage> list, bool isShowNum, bool isShowId, int SumMax, LabelX pageSum, LabelX numMax, TextBoxX pageIndex, TextBoxX numSum, ISearch isearch)
        {
            numSumMax = SumMax;
            lblNumMax = numMax;
            lblPageSum = pageSum;
            txtNumSum = numSum;
            txtPageIndex = pageIndex;
            dgv = dataGridView;
            isNum = isShowNum;
            isID = isShowId;
            iSearch = isearch;
            InitPaging(list);
        }

        public PaginManager(IPageForm ipageForm) : this(ipageForm, null) { }

        public PaginManager(IPageForm ipageForm, ISearch isearch)
        {
            numSumMax = ipageForm.SumMax;
            lblNumMax = ipageForm.numMax;
            lblPageSum = ipageForm.pageSum;
            txtNumSum = ipageForm.numSum;
            txtPageIndex = ipageForm.pageIndex;
            dgv = ipageForm.dataGridView;
            InitPaging(ipageForm.list);
            isNum = ipageForm.num;
            isID = ipageForm.id;
            iSearch = isearch;
        }

        /// <summary>
        /// 需要绑定的DataGridView
        /// </summary>
        public DataGridViewX dgv;
        /// <summary>
        /// 需要显示的总页数控件
        /// </summary>
        public LabelX lblPageSum;
        /// <summary>
        /// 需要显示的总条数控件
        /// </summary>
        public LabelX lblNumMax;
        /// <summary>
        /// 需要显示的当前页数控件
        /// </summary>
        public TextBoxX txtPageIndex;
        /// <summary>
        /// 需要显示的每页条数控件
        /// </summary>
        public TextBoxX txtNumSum;

        /// <summary>
        /// 查询接口
        /// </summary>
        public ISearch iSearch;
        /// <summary>
        /// 全部的集合
        /// </summary>
        public List<IPage> allList;
        /// <summary>
        /// 被筛选的结果
        /// </summary>
        public List<IPage> listItemIndex;
        /// <summary>
        /// 查询的结果集
        /// </summary>
        public List<IPage> listItem;

        /// <summary>
        /// 是否需要显示序列
        /// </summary>
        public bool isNum;
        /// <summary>
        /// 是否需要显示ID
        /// </summary>
        public bool isID;
        /// <summary>
        /// 当前页
        /// </summary>
        public int pageIndex;
        /// <summary>
        /// 总页数
        /// </summary>
        public int pageSum;
        /// <summary>
        /// 每页条数
        /// </summary>
        public int numSum;
        /// <summary>
        /// 记录条数
        /// </summary>
        public int numMax;
        /// <summary>
        /// 每页条数的限制数
        /// </summary>
        public int numSumMax;

        /// <summary>
        /// 当前页临时变量
        /// </summary>
        public int pageIndexN;
        /// <summary>
        /// 每页条数临时变量
        /// </summary>
        private int numSumN;


        private int n;


        /// <summary>
        /// int.TryParse 临时变量
        /// </summary>
        private int t;

        /// <summary>
        /// 初始化数据
        /// </summary>
        public void InitPaging(List<IPage> list)
        {
            allList = list;//全部的集合     *TAT*
            listItem = allList;//初始显示的集合就是全部,如果默认不是,请做筛选     *TAT*
            pageIndexN = 1;//默认当前页
            BindPagingLBL();
            BindPaging();
        }
        /// <summary>
        /// 绑定DataGridView数据
        /// </summary>
        private void BindPaging()//绑定数据显示
        {
            if (listItem == null || listItem.Count == 0)//账号作业出错时临时添加的---陈苹
            {
                dgv.Rows.Clear();
                lblPageSum.Text = "/" + 1;
                lblNumMax.Text = "/ " + 1;//数据刷新,求稳定,可删
                return;
            }
            dgv.Rows.Clear();//清空数据     *TAT*
            DataGridViewRow row;
            IPage item;
            string[] items;
            int initItemI = isID ? 0 : 1;
            for (int n = 0, i = (pageIndex - 1) * numSum; i < numMax && n < numSum; i++, n++)//n为计数器
            {
                item = listItem[i];
                row = new DataGridViewRow();
                items = item.Export();
                if (isNum)
                {
                    row.Cells.Add(new DataGridViewTextBoxCell//非数据,排版ID
                    {
                        Value = i + 1
                    });
                }
                for (int itemI = initItemI; itemI < items.Length; itemI++)
                {
                    row.Cells.Add(new DataGridViewTextBoxCell//数据     *TAT*
                    {
                        Value = items[itemI]

                    });
                }
                row.Tag = item;//Tag 方便操作
                dgv.Rows.Add(row);//添加行     *TAT*
            }
            lblNumMax.Text = "/ " + listItem.Count;//数据刷新,求稳定,可删
        }
        /// <summary>
        /// 绑定文字显示
        /// </summary>
        private void BindPagingLBL()
        {
            if (listItem == null || listItem.Count == 0) return;
            numMax = listItem.Count;//筛选之后的数据数量

            //进行判断,非数字
            numSum = int.Parse(txtNumSum.Text);//界面填写每页显示数
            pageIndex = int.Parse(txtPageIndex.Text);//当前页
            //结束判断

            if (numSum != 0)
            {
                pageSum = numMax / numSum + ((numMax % numSum == 0) ? 0 : 1);//计算总页数
                lblPageSum.Text = "/ " + pageSum;//显示总页数
                lblNumMax.Text = "/ " + numMax;//显示总条数
            }
            else
            {
                lblPageSum.Text = "/ 0";
                lblNumMax.Text = "/ 0";
            }
        }

        /// <summary>
        /// 首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void lblFirst_Click(object sender, EventArgs e)
        {
            if (txtPageIndex.Text == "1" || txtPageIndex.Text == "0")
            {
                return;
            }
            txtPageIndex.Text = "1";
            BindPagingLBL();
            BindPaging();
        }
        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void lblPageUp_Click(object sender, EventArgs e)
        {
            if (txtPageIndex.Text == "1" || txtPageIndex.Text == "0")
            {
                return;
            }
            txtPageIndex.Text = int.Parse(txtPageIndex.Text) - 1 + "";
            BindPagingLBL();
            BindPaging();
        }
        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void lblPageDown_Click(object sender, EventArgs e)
        {
            if (int.Parse(txtPageIndex.Text) > pageSum || pageSum == 0)
            {
                return;
            }
            txtPageIndex.Text = int.Parse(txtPageIndex.Text) + 1 + "";
            BindPagingLBL();
            BindPaging();
        }
        /// <summary>
        /// 末页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void lblLast_Click(object sender, EventArgs e)
        {
            if (int.Parse(txtPageIndex.Text) > pageSum || pageSum == 0)
            {
                return;
            }
            txtPageIndex.Text = pageSum.ToString();
            BindPagingLBL();
            BindPaging();
        }
        /// <summary>
        /// 当前页数文本框 TextChanged
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void txtPageIndex_TextChanged(object sender, EventArgs e)
        {
            int textPosition = txtPageIndex.SelectionStart;
            if (txtPageIndex.Text == "")
            {
                return;
            }
            if (int.TryParse(txtPageIndex.Text, out t))
            {
                pageIndexN = t;
                return;
            }
            txtPageIndex.Text = pageIndexN.ToString();
            if (textPosition - 1 < txtPageIndex.Text.Length)
            {
                txtPageIndex.SelectionStart = textPosition - 1;
            }
            else
            {
                txtPageIndex.SelectionStart = txtPageIndex.Text.Length;
            }
        }
        /// <summary>
        /// 每页条数文本框 TextChanged
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void txtNumSum_TextChanged(object sender, EventArgs e)
        {
            int textPosition = txtNumSum.SelectionStart;
            if (txtNumSum.Text == "")
            {
                return;
            }
            if (int.TryParse(txtNumSum.Text, out t))
            {
                numSumN = t;
                return;
            }
            txtNumSum.Text = numSumN.ToString();
            if (textPosition - 1 < txtNumSum.Text.Length)
            {
                txtNumSum.SelectionStart = textPosition - 1;
            }
            else
            {
                txtNumSum.SelectionStart = txtNumSum.Text.Length;
            }
        }
        /// <summary>
        /// 当前页数文本框 Leave
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void txtPageIndex_Leave(object sender, EventArgs e)
        {
            int num=0;
            if (int.TryParse(txtPageIndex.Text, out num) && num != 0)
            {
                if (num > pageSum)
                {
                    txtPageIndex.Text = pageSum.ToString();
                }
                else
                {
                    txtPageIndex.Text = num.ToString();
                }
            }
            else
            {
                txtPageIndex.Text = "1";
            }
            BindPagingLBL();
            BindPaging();

        }
        /// <summary>
        /// 每页条数文本框 Leave
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void txtNumSum_Leave(object sender, EventArgs e)
        {
            n = int.Parse(txtNumSum.Text);
            if (n > numMax || n > numSumMax)
            {
                txtNumSum.Text = (numMax > numSumMax) ? numSumMax.ToString() : numMax.ToString();//设置每页最大值
            }
            if (n == 0)
            {
                txtNumSum.Text = "1";
            }
            txtPageIndex.Text = "1";
            BindPagingLBL();
            BindPaging();
        }
        /// <summary>
        /// 当前页数文本框 KeyDown
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void txtNumSum_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up:
                    e.Handled = true;
                    n = int.Parse(txtNumSum.Text) + 1;
                    if (n > numMax || n > numSumMax)
                    {
                        txtNumSum.Text = (numMax > numSumMax) ? numSumMax.ToString() : numMax.ToString();//设置每页最大值
                        break;
                    }
                    txtNumSum.Text = n.ToString();
                    txtNumSum.SelectionStart = txtNumSum.Text.Length;
                    break;
                case Keys.Down:
                    e.Handled = true;
                    n = int.Parse(txtNumSum.Text) - 1;
                    txtNumSum.Text = (n < 1) ? "1" : n.ToString();
                    txtNumSum.SelectionStart = txtNumSum.Text.Length;
                    break;
                case Keys.Enter:
                    txtNumSum_Leave(sender, null);
                    break;
            }
        }
        /// <summary>
        /// 每页条数文本框 KeyDown
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void txtPageIndex_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up:
                    e.Handled = true;
                    lblPageUp_Click(sender, null);
                    txtPageIndex.SelectionStart = txtPageIndex.Text.Length;
                    break;
                case Keys.Down:
                    e.Handled = true;
                    lblPageDown_Click(sender, null);
                    txtPageIndex.SelectionStart = txtPageIndex.Text.Length;
                    break;
                case Keys.Enter:
                    txtPageIndex_Leave(sender, null);
                    break;
            }
        }
        /// <summary>
        /// 查询按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btnSearch_Click(object sender, EventArgs e)//查询
        {
            listItemIndex = iSearch.Search(allList);
            this.selectItemSelection(listItemIndex);
        }

        #region ISelection 成员

        public List<IPage> GetListOrder()//传输被筛选的结果
        {
            return allList;
        }

        public void selectItemSelection(List<IPage> listItem)//调用方法搜索
        {
            this.listItem = listItem;
            txtPageIndex.Text = "1";
            BindPagingLBL();
            BindPaging();
        }

        #endregion
    }

posted @ 2010-12-20 10:35  紫荆飞翔  阅读(463)  评论(2编辑  收藏  举报