using System; 
 | 
using System.Collections.Generic; 
 | 
using System.Text; 
 | 
using System.Windows.Forms; 
 | 
using System.Drawing; 
 | 
  
 | 
namespace Pub_Class 
 | 
{ 
 | 
    /// <summary> 
 | 
    /// 单据 合计网格 
 | 
    /// </summary> 
 | 
    public class ClsGridViewSum 
 | 
    { 
 | 
        public DataGridView ogrdMain = new DataGridView(); 
 | 
        public DataGridView oGridsum = new DataGridView(); 
 | 
        private string sTotalCols; 
 | 
        private string sAllowEdit; 
 | 
        public Cell OldCell = new Cell(); 
 | 
        public bool Changelock; 
 | 
        public bool Gdtlock; 
 | 
        public bool EditStatus; 
 | 
        public bool DisplayNoCol; 
 | 
        public Int32 NoCol; 
 | 
        public string KeyItem; 
 | 
        public bool NotAllowADDRow; 
 | 
        public bool NotAllowDELRow; 
 | 
        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  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 void SetGridMain() 
 | 
        { 
 | 
            for(int i=0;i<ogrdMain.Columns.Count;i++) 
 | 
            { 
 | 
                if (this.FindAllowEditCol(i)) 
 | 
                { 
 | 
                    ogrdMain.Columns[i].DefaultCellStyle.BackColor = Color.White; 
 | 
                } 
 | 
                else 
 | 
                { 
 | 
                    ogrdMain.Columns[i].DefaultCellStyle.BackColor = Color.WhiteSmoke; 
 | 
                } 
 | 
            } 
 | 
            ogrdMain.EnableHeadersVisualStyles = false;  
 | 
            ogrdMain.ColumnHeadersHeight = 40;               //标题行高 
 | 
            ogrdMain.ColumnHeadersDefaultCellStyle.BackColor = Color.LightSteelBlue; 
 | 
            ogrdMain.Columns[NoCol].DefaultCellStyle.BackColor = Color.LightSteelBlue; 
 | 
            ogrdMain.Columns[NoCol].DefaultCellStyle.SelectionBackColor = Color.LightSteelBlue; 
 | 
            ogrdMain.Columns[NoCol].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; 
 | 
        } 
 | 
  
 | 
        //设置主网格属性 红字 
 | 
        public void SetGridRedMain() 
 | 
        { 
 | 
            for (int i = 0; i < ogrdMain.Columns.Count; i++) 
 | 
            { 
 | 
                if (this.FindAllowEditCol(i)) 
 | 
                { 
 | 
                    ogrdMain.Columns[i].DefaultCellStyle.BackColor = Color.White; 
 | 
                } 
 | 
                else 
 | 
                { 
 | 
                    ogrdMain.Columns[i].DefaultCellStyle.BackColor = Color.WhiteSmoke; 
 | 
                } 
 | 
            } 
 | 
            ogrdMain.EnableHeadersVisualStyles = false; 
 | 
            ogrdMain.ColumnHeadersHeight = 40;               //标题行高 
 | 
            ogrdMain.ColumnHeadersDefaultCellStyle.BackColor = Color.LightPink; 
 | 
            ogrdMain.Columns[NoCol].DefaultCellStyle.BackColor = Color.LightPink; 
 | 
            ogrdMain.Columns[NoCol].DefaultCellStyle.SelectionBackColor = Color.LightPink; 
 | 
            ogrdMain.Columns[NoCol].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; 
 | 
        } 
 | 
  
 | 
        //设置合计网格属性  幻心 
 | 
        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].Cells[0].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.Width = ogrdMain.Width ; 
 | 
            RefreshNoCol(); 
 | 
        } 
 | 
        //合计函数 
 | 
        public void Total() 
 | 
        { 
 | 
            string[] Cols; 
 | 
            Int32 i, j, ColsTotal; 
 | 
            if(ClsPub.isStrNull( sTotalCols).ToString().Trim()==""  ) 
 | 
                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.RowCount; 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 LeaveCell() 
 | 
        { 
 | 
            if (Changelock) 
 | 
                return; 
 | 
            OldCell.Col = ogrdMain.CurrentCell.ColumnIndex; 
 | 
            OldCell.Row = ogrdMain.CurrentRow.Index; 
 | 
        } 
 | 
        //列是否允许编辑 
 | 
        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  ) 
 | 
            { 
 | 
                return; 
 | 
            } 
 | 
            if (GetNullRows() < 1) 
 | 
            {  
 | 
                ogrdMain.Rows.Insert(ogrdMain.CurrentRow.Index,1); 
 | 
                if (ogrdMain.CurrentRow.Index != ogrdMain.RowCount-1) 
 | 
                { 
 | 
                      
 | 
                    DisplayCurRow();  
 | 
                    RefreshNoCol(); 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
        //删除行 
 | 
        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; 
 | 
        } 
 | 
        //获得 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 DisplayCurRow() 
 | 
        { 
 | 
            Int64 Toprowte; 
 | 
            if (CountNullRows() < 20) 
 | 
                ogrdMain.Rows.Add(); 
 | 
            Toprowte = 0; 
 | 
        } 
 | 
        //计算用户可用行 
 | 
        public Int64 CountNullRows() 
 | 
        { 
 | 
            Int32 r; 
 | 
            Int32 lngRows; 
 | 
            lngRows = 0; 
 | 
            for (r = 0; r < ogrdMain.RowCount; r++) 
 | 
            { 
 | 
                if (ClsPub.isStrNull( ogrdMain.Rows[r].Cells[0].Value)!= "*") 
 | 
                    lngRows = lngRows + 1; 
 | 
            } 
 | 
            return lngRows; 
 | 
        } 
 | 
  
 | 
        //显示当前列 
 | 
        public void DisplayCurCol() 
 | 
        { 
 | 
            //Int64 Toprowte; 
 | 
            //Toprowte = 0; 
 | 
            //while (ogrdMain.CellTop+ogrdMain.get_RowHeight(ogrdMain.Row)+ogrdMain.RowHeightMin>ogrdMain.Size.Height   &&  ogrdMain.TopRow!=Toprowte) 
 | 
            //{ 
 | 
            //    Toprowte=ogrdMain.TopRow; 
 | 
            //    ogrdMain.TopRow=ogrdMain.TopRow+1; 
 | 
            //} 
 | 
            //Toprowte = 0; 
 | 
            //while (ogrdMain.CellTop < ogrdMain.FixedRows * ogrdMain.get_RowHeight(0) && ogrdMain.TopRow != Toprowte) 
 | 
            //{ 
 | 
                 
 | 
            //    Toprowte = ogrdMain.TopRow; 
 | 
            //    if (ogrdMain.TopRow > 1) 
 | 
            //    { 
 | 
            //        ogrdMain.TopRow = ogrdMain.TopRow -1; 
 | 
            //    } 
 | 
            //} 
 | 
        } 
 | 
        //public void CheckRowData() 
 | 
        //{ 
 | 
        //    int r; 
 | 
        //    if (ogrdMain.Row >= ogrdMain.FixedRows) 
 | 
        //    { 
 | 
        //        if (ClsPub.isStrNull(ogrdMain.get_TextMatrix(ogrdMain.Row, 0).ToString()) != "*") 
 | 
        //        { 
 | 
        //            for ( r = ogrdMain.FixedRows; r < ogrdMain.Rows; r++) 
 | 
        //            { 
 | 
        //                if (ClsPub.isStrNull(ogrdMain.get_TextMatrix(r, 0).ToString()) != "*") 
 | 
        //                { 
 | 
        //                    if (NotAllowADDRow) 
 | 
        //                    { 
 | 
        //                        Changelock = true; 
 | 
        //                        ogrdMain.Select(r - 1, ogrdMain.Col); 
 | 
        //                        Changelock = false; 
 | 
        //                        return; 
 | 
        //                    } 
 | 
        //                    else 
 | 
        //                    { 
 | 
        //                        break; 
 | 
        //                    } 
 | 
        //                } 
 | 
        //            } 
 | 
        //            // 
 | 
        //            if (r <= ogrdMain.Rows - 1) 
 | 
        //            { 
 | 
        //                Changelock = true; 
 | 
        //                ogrdMain.Select(r , ogrdMain.Col); 
 | 
        //                Changelock = false; 
 | 
        //            } 
 | 
        //            else 
 | 
        //            { 
 | 
        //                Changelock = true; 
 | 
        //                ogrdMain.Select(ogrdMain.Rows - 1, ogrdMain.Col); 
 | 
        //                Changelock = false; 
 | 
        //            } 
 | 
        //        } 
 | 
        //        DisplayCurRow(); 
 | 
        //    } 
 | 
        //} 
 | 
        //刷新行号 
 | 
        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; 
 | 
        } 
 | 
        // 
 | 
    } 
 | 
} 
 |