DataTable分页实例,

DateTable分页通用类:

   public static class Page {

        /// 
        /// DataTable分页
        /// 
        /// DataTable
        /// 页索引,注意:从1开始
        /// 每页大小
        /// 分好页的DataTable数据              第1页        每页10条
        public static DataTable GetPagedTable(DataTable dt, int PageIndex, int PageSize)
        {
            if (PageIndex == 0) { return dt; }
            DataTable newdt = dt.Copy();
            newdt.Clear();
            int rowbegin = (PageIndex - 1) * PageSize;
            int rowend = PageIndex * PageSize;

            if (rowbegin >= dt.Rows.Count)
            { return newdt; }

            if (rowend > dt.Rows.Count)
            { rowend = dt.Rows.Count; }
            for (int i = rowbegin; i <= rowend - 1; i++)
            {
                DataRow newdr = newdt.NewRow();
                DataRow dr = dt.Rows[i];
                foreach (DataColumn column in dt.Columns)
                {
                    newdr[column.ColumnName] = dr[column.ColumnName];
                }
                newdt.Rows.Add(newdr);
            }
            return newdt;
        }

        /// 
        /// 返回分页的页数
        /// 
        /// 总条数
        /// 每页显示多少条
        /// 如果 结尾为0:则返回1
        public static int PageCount(int count, int pageye)
        {
            int page = 0;
            int sesepage = pageye;
            if (count % sesepage == 0) { page = count / sesepage; }
            else { page = (count / sesepage) + 1; }
            if (page == 0) { page += 1; }
            return page;
        }
    }
具体的使用:

 1. 创建分页需要的属性以及类:

 DataTable dbtb = new DataTable();
        int pageSize = 20;     //每页显示行数
        int nMax = 0;         //总记录数
        int pageCount = 0;    //页数=总记录数/每页显示行数
        int pageCurrent = 1;   //当前页号

2.初始化加载>页面加载事件:

dbtb.Columns.Add("序号", typeof(int));
            dbtb.Columns.Add("操作", typeof(string));
            dbtb.Columns.Add("用户", typeof(string));
            dbtb.Columns.Add("昵称", typeof(string));
            dbtb.Columns.Add("操作时间", typeof(string));
            adduserFromDB("");


            dgv_Log.Rows.Clear();
            LodDate(dbtb, 1, pageSize);
            this.txtCurrentPage.Text = "1";

3:调用的方法:

   public void adduserFromDB(string sqlParam)
        {
            dbtb.Rows.Clear();
            dgv_Log.Rows.Clear();
            System.Collections.Generic.List loglist = Core.User.GetLogList(sqlParam);
            foreach (Entity.LogItem log in loglist)
            {
                int index = this.dgv_Log.Rows.Add();
                this.dgv_Log.Rows[index].Cells["ID"].Value = log.id;
                this.dgv_Log.Rows[index].Cells["data"].Value = log.Data;
                this.dgv_Log.Rows[index].Cells["UserCode"].Value = log.UserCode;
                this.dgv_Log.Rows[index].Cells["UserName"].Value = log.UserName;
                this.dgv_Log.Rows[index].Cells["Datetime"].Value = log.Datetime;
                DataRow newRow = dbtb.NewRow();
                newRow["序号"] = log.id;
                newRow["操作"] = log.Data;
                newRow["用户"] = log.UserCode;
                newRow["昵称"] = log.UserName;
                newRow["操作时间"] = log.Datetime;
                dbtb.Rows.Add(newRow);
            }
            LodDate(dbtb, 1, pageSize);
        }
        //  加载 
        public void LodDate(DataTable dbt, int PageIndex, int PageSize)
        {
            //pageSize = 10;     //每页显示行数
            nMax = dbt.Rows.Count;     //总记录数
            pageCount = Core.Page.PageCount(nMax,pageSize);    //页数=总记录数/每页显示行数
            pageCurrent = PageIndex;   //当前页号
            //nCurrent = 0;      //当前记录行
            DataTable dt = Core.Page.GetPagedTable(dbt, PageIndex, PageSize);
            this.labelX3.Text = dbt.Rows.Count.ToString();
            this.labelX4.Text = PageSize.ToString();
            this.lblPageCount.Text = (Core.Page.PageCount(dbt.Rows.Count, PageSize)).ToString();
            this.txtCurrentPage.Text = PageIndex.ToString();
            //this.lblPageCount.Text = (Convert.ToInt32(dbt.Rows.Count) / PageSize).ToString();

            this.dgv_Log.Rows.Clear();
            //for (int i = 0; i < dt.Columns.Count; i++)
            //{
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    //temp = dt.Rows[j][i].ToString();//具体情况具体设置
                    int index = this.dgv_Log.Rows.Add();
                    this.dgv_Log.Rows[index].Cells["ID"].Value = dt.Rows[j][0].ToString();
                    this.dgv_Log.Rows[index].Cells["data"].Value = dt.Rows[j][1].ToString();
                    this.dgv_Log.Rows[index].Cells["UserCode"].Value = dt.Rows[j][2].ToString();
                    this.dgv_Log.Rows[index].Cells["UserName"].Value = dt.Rows[j][3].ToString();
                    this.dgv_Log.Rows[index].Cells["Datetime"].Value = dt.Rows[j][4].ToString();
                }           
            //}
           
        }

4:第一页

 dgv_Log.Rows.Clear();
            LodDate(dbtb, 1, pageSize);
            this.txtCurrentPage.Text = "1";


5:最后一页

 dgv_Log.Rows.Clear();
            LodDate(dbtb, pageCount, pageSize);
            this.txtCurrentPage.Text = pageCount.ToString();

6:上一页

if (pageCurrent >1)
            {
                pageCurrent--;
                LodDate(dbtb, pageCurrent, pageSize);
                this.txtCurrentPage.Text = pageCurrent.ToString();
            }else 
            {
                MessageBox.Show("已经是第一页,请点击“下一页”查看!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }          


7:下一页

  if (pageCount > pageCurrent)
            {
                pageCurrent++;
                LodDate(dbtb, pageCurrent, pageSize);
                this.txtCurrentPage.Text = pageCurrent.ToString();
            }
            else
            {
                MessageBox.Show("已经是最后一页,请点击“上一页”查看!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }



8:删除的时候需要从Datatable删除查找到的行( 删除的时候需要更新状态)

 //从DataTable中去掉指定的行
                        DataRow[] foundRow = dbtb.Select("序号=" + id);
                        foreach (DataRow row in foundRow)
                        {
                            dbtb.Rows.Remove(row);
                        }
 //更新底部状态
 this.labelX3.Text = dbtb.Rows.Count.ToString();
                    this.labelX4.Text = pageSize.ToString();
                    this.lblPageCount.Text = (Core.Page.PageCount(dbtb.Rows.Count, pageSize)).ToString();
                    //this.txtCurrentPage.Text = PageIndex.ToString();



你可能感兴趣的:(winform,winfrom)