C#Winform:DataGridView 简单分页实现

首先用toolstrip控件建立一个分页用的按钮,如下图


本人不怎么会描述,直接上代码:

Code
 1/**//// <summary>
 2        /// 获取总页数
 3        /// </summary>
 4        /// <param name="recordcount">总记录数</param>
 5        /// <param name="page_size"></param>
 6        /// <returns></returns>

 7        public static int GetPageCount(int recordcount, int page_size)
 8        {
 9            int pagecount = 0;
10            if (recordcount % page_size == 0)
11                pagecount = recordcount / page_size;
12            else
13                pagecount = (recordcount / page_size) + 1;
14
15            return pagecount;
16        }

17
18        /**//// <summary>
19        /// 获取分页数据
20        /// </summary>
21        /// <param name="kl2">DATASET</param>
22        /// <param name="page_size">每页记录数</param>
23        /// <param name="pageindex">页索引</param>
24        /// <param name="page_sum">总页数</param>
25        /// <returns></returns>

26        public static DataView GetPagerForView(DataTable dt, int page_size, int pageindex,out string msg)
27        {
28
29            DataView dv = new DataView();
30
31            if (dt != null)
32            {
33                int recordCount = dt.Rows.Count; //总记录数
34                int page_sum = GetPageCount(recordCount, page_size);
35                if (page_size < dt.Rows.Count)//kl2 :SQL查询函数返回的DATASET   
36                {
37                    if (page_size == 0)//text_intpase :判断用户设置的分页是否合法
38                        page_size = 10;
39                    //recordCount = kl2.Tables[0].Rows.Count;//假设每页只显示1条数据,则共可以显示的页数:pagemark页
40                    if (page_size < 1)
41                    {
42                        msg = "请将分页项设置在[1-" + dt.Rows.Count.ToString() + "]之间";
43                    }

44
45
46                    msg = "" + page_sum.ToString() + "页," + dt.Rows.Count.ToString() + "";//page_num :lable
47                    DataTable page_table = new DataTable();//记录当前正在操作的是哪个表,全局变量,值由查询函数获取
48
49                    for (int k = 0; k < dt.Columns.Count; k++)
50                    {
51                        page_table.Columns.Add(dt.Columns[k].ColumnName);
52                    }

53                    if (dt.Rows.Count != 0 && page_size < dt.Rows.Count)
54                    {
55                        page_table.Clear();
56                        try    //普通页面显示
57                        {
58                            page_table.Clear();
59                            for (int i = 0; i < page_size; i++)
60                            {
61                                page_table.Rows.Add(dt.Rows[i + (pageindex * page_size)].ItemArray);
62                            }

63                        }

64                        catch //最后不足一个页面的显示
65                        {
66                            page_table.Clear();
67                            try
68                            {
69                                for (int s = 0; s < recordCount - (pageindex * page_size); s++)
70                                {
71                                    page_table.Rows.Add(dt.Rows[s + (pageindex * page_size)].ItemArray);
72                                }

73                            }

74                            catch { }
75                        }

76                        msg += " 当前第" + (pageindex + 1).ToString() + "";
77                    }

78                    dv = page_table.DefaultView;
79                }

80                else
81                {
82                    dv = dt.DefaultView;
83                    msg = "共1页," + dt.Rows.Count.ToString() + "";
84                    msg += " 当前第" + (pageindex + 1).ToString() + "";
85
86                }

87                return dv;
88            }

89            else
90            {
91                msg = "没有数据!";
92                return null;
93            }

94        }


Form中调用:

Code
 1private DataTable GetDicData()//获取所有据
 2        {
 3            BLLB_CommunitDIC bcdicBll = new BLLB_CommunitDIC();
 4            if (trvDic.SelectedNode != null)
 5            {
 6                if (trvDic.SelectedNode.Parent == null)
 7                {
 8                    return null;
 9                }

10                else
11                {
12                    int areaid = Convert.ToInt32(trvDic.SelectedNode.Tag);
13
14                    return FCT.Common.PublicMethods.ConvertToDataSet(bcdicBll.GetAll(areaid)).Tables[0];
15                }

16            }

17            else
18                return null;
19        }

20//根据页索此获取数据
21        private void InitData(int pageindex)
22        {
23            DataTable dt = GetDicData();
24            string msg;
25            if (dt != null && dt.Rows.Count > 0)
26            {
27                dgrvDic.AutoGenerateColumns = false;
28                dgrvDic.DataSource = Public.CustomerPager.GetPagerForView(dt, 10, pageindex, out msg);
29                toolStripLabel18.Text = msg;
30            }

31        }

Code
 1分页事件#region 分页事件
 2        private void toolStrip7_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
 3        {
 4            if (e.ClickedItem.Tag != null)
 5            {
 6                this.Cursor = Cursors.WaitCursor;
 7                int _currentPageIndex = 1;
 8                DataTable dt = GetDicData();
 9                int pagecount = 0;
10                if (dt != null)
11                    pagecount = Public.CustomerPager.GetPageCount(dt.Rows.Count, 10);
12
13                if (toolStripTextBox6.Text != "")
14                {
15                    try
16                    {
17                        _currentPageIndex = Convert.ToInt32(toolStripTextBox6.Text);
18                    }

19                    catch
20                    {
21                        _currentPageIndex = 1;
22                    }

23                }

24
25
26
27                switch (e.ClickedItem.Tag.ToString())
28                {
29                    case "First":
30                        _currentPageIndex = 1;
31                        break;
32                    case "Prev":
33                        _currentPageIndex = _currentPageIndex - 1;
34                        break;
35                    case "Next":
36                        _currentPageIndex = _currentPageIndex + 1;
37                        break;
38                    case "Last":
39                        _currentPageIndex = pagecount;
40                        break;
41                    default:
42                        this.Cursor = Cursors.Default;
43                        break;
44                }

45
46                InitData(_currentPageIndex - 1);
47
48                if (_currentPageIndex == 1)
49                {
50                    toolStripTextBox6.Text = "1";
51                    this.toolStripButton25.Enabled = false;
52                    this.toolStripButton26.Enabled = false;
53                    this.toolStripButton27.Enabled = true;
54                    this.toolStripButton28.Enabled = true;
55                }

56                else
57                {
58                    if (_currentPageIndex < pagecount && _currentPageIndex >= 1)
59                    {
60                        toolStripTextBox6.Text = _currentPageIndex.ToString();
61                        this.toolStripButton25.Enabled = true;
62                        this.toolStripButton26.Enabled = true;
63                        this.toolStripButton27.Enabled = true;
64                        this.toolStripButton28.Enabled = true;
65                    }

66                    else if (_currentPageIndex == pagecount)
67                    {
68                        toolStripTextBox6.Text = pagecount.ToString();
69                        this.toolStripButton25.Enabled = true;
70                        this.toolStripButton26.Enabled = true;
71                        this.toolStripButton27.Enabled = false;
72                        this.toolStripButton28.Enabled = false;
73                    }

74                    else
75                    {
76                        this.toolStripButton25.Enabled = false;
77                        this.toolStripButton26.Enabled = false;
78                        this.toolStripButton27.Enabled = false;
79                        this.toolStripButton28.Enabled = false;
80                    }

81                }

82                this.Cursor = Cursors.Default;
83            }

84        #endregion

/** toolStripButton25:首页  toolStripButton26:上一页 toolStripButton27:下一页   toolStripButton28:末页 **/


写的有点乱,希望各位能明白,呵呵~~~

转载于:https://www.cnblogs.com/hxling/archive/2008/04/22/1165980.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值