yangyin
2025-02-28 baa80d650adebcce70f1113cc1020c6039c159a0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
 
namespace DPumpHydr.WinFrmUI.WenSkin.Controls.Paging
{
    public class WenDataTablePaging : WenPaging
    {
        public WenDataTablePaging() : base()
        {
        }
 
        #region 公有属性
 
        private PagingPageCollection items;
        private object dataSource;
 
        #endregion
 
        #region 公有属性
 
        [Category("Wen"), Description("获取或设置每一页设置数量数量"), DefaultValue(100)]
        public int PageRowCount { get; set; } = 100;
 
        [Category("Wen"), Description("获取设置单项数据"), DefaultValue(null)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Editor(typeof(Design.Editor.WenCollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]
        [EditorBrowsable(EditorBrowsableState.Always)]
        public PagingPageCollection Items => items ??= new PagingPageCollection(this);
        public object DataSource { get => dataSource; set { dataSource = value; DataBind(); } }
 
        #endregion
 
        private void DataBind()
        {
            Items.Clear();
            if (dataSource is DataTable dt)
            {
                //向上取整
                MaxPageCout = (int)Math.Ceiling((decimal)dt.Rows.Count / PageRowCount);
                for (int i = 1; i <= MaxPageCout; i++)
                {
                    List<DataRow> dataRows = new List<DataRow>();
                    for (int r = PageRowCount * (i - 1); r < (i * PageRowCount > dt.Rows.Count ? dt.Rows.Count : i * PageRowCount); r++)
                    {
                        dataRows.Add(dt.Rows[r]);
                    }
                    PagingPage paging = new PagingPage()
                    {
                        PageIndex = i,
                        Value = dataRows.ToArray(),
                    };
                    Items.Add(paging);
                }
                OnPagingButtonClick(new PagingButtonClickEventArgs(1));
            }
        }
 
        #region 重写
 
        protected override void OnPagingButtonClick(PagingButtonClickEventArgs e)
        {
            base.OnPagingButtonClick(e);
            PagingPageChange?.Invoke(this, new PagingPageChangeEventArgs(e.PageIndex, Items[e.PageIndex-1]));
        }
 
        #endregion
 
        #region 委托
 
        public delegate void PagingPageChangeEventHandler(object sender, PagingPageChangeEventArgs e);
        [Category("Wen"), Description("绑定数据值变化事件")]
        public event PagingPageChangeEventHandler PagingPageChange;
        public class PagingPageChangeEventArgs : PagingButtonClickEventArgs
        {
            private readonly PagingPage pagingPage;
 
            public PagingPageChangeEventArgs(int pageIndex, PagingPage pagingPage) : base(pageIndex)
            {
                this.pagingPage = pagingPage;
            }
            public PagingPage PagingPage => pagingPage;
        }
 
        #endregion  
 
        #region 子类
 
        [ToolboxItem(false)]
        [DesignTimeVisible(false)]
        public class PagingPage : Component
        {
            public object Value { get; set; }
            public int PageIndex { get; set; }
            public DataRow[] DataRows => Value as DataRow[];
            public DataTable DataTable
            {
                get
                {
                    DataTable dt = DataRows[0].Table.Clone();
                    foreach (DataRow dataRow in DataRows)
                    {
                        dt.Rows.Add(dataRow.ItemArray);
                    }
                    return dt;
                }
            }
            public override string ToString()
            {
                return PageIndex.ToString();
            }
        }
 
        public class PagingPageCollection : WenCollection
        {
            public PagingPageCollection(WenPaging owner)
            {
                this.owner = owner;
            }
 
            #region 私有属性
            private WenPaging owner;
            #endregion
            public PagingPage this[int index] => index < Items.Count ? Items[index] as PagingPage : null;
        }
        #endregion
    }
}