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
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
 
namespace Pub_Class
{
    /// <summary>
    /// 单网格可编辑 (除单据和列表)
    /// </summary>
    public class ClsGridViewFun
    {
        public DataGridView ogrdMain = new DataGridView();  
        private string sAllowEdit;//可编辑列 
        public bool EditStatus;
        public bool DisplayNoCol;//是否显示 序号列
        public Int32 NoCol;//序号列
        public bool Changelock;
        public string KeyItem;
        public bool NotAllowADDRow;
        public bool NotAllowDELRow;
        public char chr = Convert.ToChar(",");
        
         
        //建立 可编辑列
        public void  BuildAllowEditCols(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];
                }
            }
            sAllowEdit= tStr;
        }
 
       
        //列是否允许编辑
        public bool FindAllowEditCol(Int64 Col)
        {
            Int64 i;
            string[] s;
            if (sAllowEdit.Trim() == "")
                return false;
            s=sAllowEdit.Split(chr );
            for(i=0;i<=s.Length-1;i++)
            {
                if(Col==ClsPub.isInt(s[i]))
                    return true;
            }
            return false;
        }
 
        //增行
        public void Sub_AddRow()
        { 
            if (ogrdMain.CurrentRow==null  )
            {
                ogrdMain.Rows.Add();
                return;
            }
            if (GetNullRows() < 1)
            { 
                ogrdMain.Rows.Insert(ogrdMain.CurrentRow.Index,1);
                if (ogrdMain.CurrentRow.Index != ogrdMain.RowCount-1)
                {
                    DisplayCurRow(); 
                    RefreshNoCol();
                }
            }
        }
 
        //获得 NULL 行
        public Int64 GetNullRows()
        {
            Int32 r, c;
            Int32 lngRows;
            lngRows = 0;
            for (r = 0; r < ogrdMain.RowCount; r++)
            {
                if (ClsPub.isStrNull(ogrdMain.Rows[r].Cells[0].Value) == "*")
                {
                    if (DisplayNoCol == true)
                    {
                        for (c = NoCol + 1; c < ogrdMain.ColumnCount; c++)
                        {
                            if (ClsPub.isStrNull(ogrdMain.Rows[r].Cells[c].Value) != "")
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        for (c = 1; c < ogrdMain.ColumnCount; c++)
                        {
                            if (ClsPub.isStrNull(ogrdMain.Rows[r].Cells[c].Value) != "")
                            {
                                break;
                            }
                        }
                    }
                    if (c == ogrdMain.ColumnCount)
                        lngRows = lngRows + 1;
                }
            }
            return lngRows;
        }
 
        //删除行
        public void Sub_DelRow()
        {
            int Scqwghz; //原选中行
            int Scqwglz; //原选中列
      
            Scqwghz = ogrdMain.CurrentRow.Index;
            Scqwglz = ogrdMain.CurrentCell.ColumnIndex; 
            DisplayCurRow();
            Changelock = true; 
            Changelock = false;
            
            ogrdMain.CurrentRow.DefaultCellStyle.BackColor = Color.Red;
            if (MessageBox.Show( "确定要删除当前记录?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
            {
                ogrdMain.CurrentRow.DefaultCellStyle.BackColor = Color.White;
                Changelock = true; 
                Changelock = false;
                return;
            }
            ogrdMain.Rows.RemoveAt(ogrdMain.CurrentRow.Index);
            ogrdMain.Rows.Add();
            Changelock = true; 
            Changelock = false;
            RefreshNoCol(); 
            EditStatus = false;
        }
 
        
        //显示当前行
        public void DisplayCurRow()
        {
            Int64 Toprowte;
            Toprowte = 0;
        }
 
        
        //刷新行号
        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;
        }
        //
    }
}