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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
 
namespace Pub_Class
{
    /// <summary>
    /// 单据列表 合计网格
    /// </summary>
    public class ClsGridListSum
    {
        public DataGridView ogrdMain = new DataGridView();
        public DataGridView oGridsum = new DataGridView();
        private string sTotalCols;  
        public bool Changelock;  
        public bool DisplayNoCol;   
        public char chr = Convert.ToChar(",");
        
        //建立 合计列
        public void  BuildTotalCols(string[] Cols)
        {
            Int64 i;
            string tStr;
            tStr = "";
            for (i = 0; i < Cols.Length; i++)
            {
                if (i == 0)
                {
                    tStr = Cols[i];
                }
                else
                {
                    tStr = tStr + "," + Cols[i];
                }
            }
            sTotalCols= tStr;
        }
         
        //设置主网格属性
        public void SetGridMain(string Name)
        {
            ogrdMain.RowTemplate.Height = 18;
            ogrdMain.RowTemplate.MinimumHeight = 18;
            ogrdMain.ColumnHeadersHeight = 40;
            ogrdMain.RowHeadersVisible = false;
            ogrdMain.ColumnHeadersVisible = true;
            //格式化
            //表格设置
            ogrdMain.RowsDefaultCellStyle.SelectionForeColor = Color.White;
            ogrdMain.RowsDefaultCellStyle.SelectionBackColor = Color.DarkCyan;
            ogrdMain.DefaultCellStyle.SelectionBackColor = Color.DarkCyan;
            ogrdMain.DefaultCellStyle.SelectionForeColor = Color.White;
            ogrdMain.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            ogrdMain.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            ogrdMain.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            ogrdMain.ReadOnly = true;
            ogrdMain.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing; //允许用户调整列宽
            ogrdMain.EnableHeadersVisualStyles = false;     //网格标题 样式
            ogrdMain.AllowUserToAddRows = false;             //是否允许自增--否
            ogrdMain.AllowUserToDeleteRows = false;          //是否允许删除--否 
            ogrdMain.AllowUserToResizeColumns = true;         //允许调整列宽--是
            ogrdMain.AllowUserToResizeRows = false;           //允许调整行高--否
            ogrdMain.AllowUserToOrderColumns = true;         //允许 拖拽列
            ogrdMain.BackgroundColor = Color.White;
            ogrdMain.GridColor = Color.LightGray;             //网格线颜色
            ogrdMain.ShowCellToolTips = true;
            ogrdMain.CellBorderStyle = DataGridViewCellBorderStyle.RaisedVertical;
            ogrdMain.ColumnHeadersDefaultCellStyle.BackColor = Color.LightGray;
            //
            ClsPub.GetGridView(ogrdMain, Name, ClsPub.AppPath);//设置列宽
        }
 
         
        //设置合计网格属性   
        public void SetGridsum( )
        {
            
            Int32 i;
            oGridsum.Enabled = false;
            oGridsum.Width  = ogrdMain.Width;
            oGridsum.RowHeadersVisible = false;
            oGridsum.ColumnHeadersVisible = false;
            oGridsum.RowCount = 1;
            oGridsum.ColumnCount = ogrdMain.ColumnCount;
            oGridsum.HorizontalScrollingOffset = ogrdMain.HorizontalScrollingOffset; 
 
            oGridsum.Rows[0].Height = 20;
 
            oGridsum.Height = 20;
            //for (i = 1; i < oGridsum.ColumnCount; i++)
            //{
            //    oGridsum.Rows[0].Cells[i].Value = "";
            //}
            
            oGridsum.Rows[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            oGridsum.Rows[0].DefaultCellStyle.BackColor = Color.Khaki;
            for (i = 0; i < ogrdMain.ColumnCount; i++)
            {
 
                oGridsum.Columns[i].Visible = ogrdMain.Columns[i].Visible;
                oGridsum.Columns[i].Width = ogrdMain.Columns[i].Width;
                oGridsum.Rows[0].Cells[i].Value = "";
            }
            oGridsum.Rows[0].Cells[0].Value = "合计";
            oGridsum.Width = ogrdMain.Width ;
            RefreshNoCol();
        }
 
 
        //  
        /// <summary>
        /// 设置合计网格属性(只调列宽不调数据) 
        /// </summary>
        public void SetGridsum_NoData()
        {
 
            Int32 i;
            oGridsum.Enabled = false;
            oGridsum.Width = ogrdMain.Width;
            oGridsum.RowHeadersVisible = false;
            oGridsum.ColumnHeadersVisible = false;
            oGridsum.HorizontalScrollingOffset = ogrdMain.HorizontalScrollingOffset;
 
            oGridsum.Rows[0].Height = 20;
 
            oGridsum.Height = 20;
            oGridsum.Rows[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            oGridsum.Rows[0].DefaultCellStyle.BackColor = Color.Khaki;
            for (i = 0; i < ogrdMain.ColumnCount; i++)
            {
                oGridsum.Columns[i].Visible = ogrdMain.Columns[i].Visible;
                oGridsum.Columns[i].Width = ogrdMain.Columns[i].Width;
            }
            oGridsum.Rows[0].Cells[0].Value = "合计";
            oGridsum.Width = ogrdMain.Width;
            RefreshNoCol();
        }
 
 
        //合计函数
        public void Total()
        {
            string[] Cols;
            Int32 i, j, ColsTotal;
            if(ClsPub.isStrNull(sTotalCols).ToString().Trim()==""  )
                return;
            if (ogrdMain.SelectedRows == null)
                return;
            Cols = sTotalCols.Split(chr);
            ColsTotal = Cols.Length;
            for (j = 0; j < ColsTotal; j++)   //清空
            {
                oGridsum.Rows[0].Cells[j].Value = "";
            }
            try
            {
                double d = 0;
                for (j = 0; j < ColsTotal; j++)
                {
                    d = 0;
                    for (i = 0; i < ogrdMain.SelectedRows.Count; i++)
                    { 
                        //d = d + ClsPub.isDoule(ogrdMain.Rows[i].Cells[ClsPub.isInt(Cols[j])].Value);
                        d = d + ClsPub.isDoule(ogrdMain.SelectedRows[i].Cells[ClsPub.isInt(Cols[j])].Value);
                    }
                    oGridsum.Rows[0].Cells[ClsPub.isInt(Cols[j])].Value=d.ToString();
                }
            }
            catch (Exception  e)
            {
                string s = "";
                s= e.Message;
            }
        }
 
        //合计函数 合计全部
        public void TotalAll()
        {
            string[] Cols;
            Int32 i, j, ColsTotal;
            if (ClsPub.isStrNull(sTotalCols).ToString().Trim() == "")
                return;
            //if (ogrdMain == null)
            //    return;
            Cols = sTotalCols.Split(chr);
            ColsTotal = Cols.Length;
            for (j = 0; j < ColsTotal; j++)   //清空
            {
                oGridsum.Rows[0].Cells[j].Value = "";
            }
            try
            {
                double d = 0;
                for (j = 0; j < ColsTotal; j++)
                {
                    d = 0;
                    for (i = 0; i < ogrdMain.Rows.Count; i++)
                    {
                        d = d + ClsPub.isDoule(ogrdMain.Rows[i].Cells[ClsPub.isInt(Cols[j])].Value);
                    }
                    oGridsum.Rows[0].Cells[ClsPub.isInt(Cols[j])].Value = d.ToString();
                }
            }
            catch (Exception e)
            {
                string s = "";
                s = e.Message;
            }
        }
        //
        //刷新行号
        public void RefreshNoCol()
        {
            //int  i, m; 
            //i = 0;
            //m = 0;
            //for (i = 0; i < ogrdMain.RowCount; i++)
            //{
            //    m = m + 1;
            //    ogrdMain.Rows[i].Cells[NoCol].Value = m.ToString().Trim();
            //}
            //ogrdMain.Columns[NoCol].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        }
        //
    }
}