C#DataGridView合計處理

來源:互聯網
上載者:User

標籤:

網上查了一些關於合計的代碼 ,但發現大都都不盡人意,就自己再根據資料改了一下。

#region 合計            //調用方法樣本            //HeJi heji = null;            //heji = new HeJi(label1,dataGridView1);            //heji.AddHeji("FTransportOperator_ID",4);            //heji.AddHeji("FLoadingTeam_ID",6);            //heji.sumdata();                public class HeJi    {                //兩個控制項 文本,表格        private Label Lab1;        private DataGridView Grid;        //定義類的私人變數,兩個是捲軸        HScrollBar hs;        VScrollBar vs;        //合計的資訊列表        List<HeJiMessage> li = new List<HeJiMessage>();        //初始化        public HeJi() { }        public HeJi(Label lab, DataGridView grid)         {            Lab1 = lab;            Grid = grid;            hs = ((HScrollBar)this.Grid.Controls[0]);            vs = ((VScrollBar)this.Grid.Controls[1]);            hs.ValueChanged += new EventHandler(hs_ValueChanged);            vs.ValueChanged += new EventHandler(vs_ValueChanged);            Lab1.Paint += new PaintEventHandler(Lab_Paint);            Grid.CellValueChanged += new DataGridViewCellEventHandler(CellValueChanged);        }        //捲軸委託事件        public void hs_ValueChanged(object sender, EventArgs e)        {            this.Lab1.Invalidate();        }        public void vs_ValueChanged(object sender, EventArgs e)        {            this.Lab1.Invalidate();        }        /// <summary>        /// 表格數值改變        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void CellValueChanged(object sender, DataGridViewCellEventArgs e)        {            sumdata();        }        /// <summary>        /// //添加需要合計的列        /// </summary>        /// <param name="name">欄位名</param>        /// <param name="number">列索引</param>        public void AddHeji(string name, int number)        {            Rectangle rec = this.Grid.GetCellDisplayRectangle(number, 0, false);            li.Add(new HeJiMessage(name, number,rec));        }        /// <summary>        /// paint委託事件,重畫        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void Lab_Paint(object sender, PaintEventArgs e)        {            DataGridViewColumnCollection Columns = this.Grid.Columns;            int count = Grid.Columns.Count;            Graphics grf = e.Graphics;            StringFormat strfmt = new StringFormat();            strfmt.Alignment = StringAlignment.Center;            //判斷有顯示的行,利用行擷取Rectangle位置資訊            foreach (HeJiMessage heji in li)            {                Rectangle rec = this.Grid.GetCellDisplayRectangle(heji.CellNumber, 0, false);                for (int i = 0; i < Grid.Rows.Count; i++)                {                    if (Grid.Rows[i].Displayed)                    {                        rec = this.Grid.GetCellDisplayRectangle(heji.CellNumber, i, false);                        heji.Rec = rec;                        Console.WriteLine("顯示行為:" + i.ToString());                        break;                    }                }                grf.DrawString(string.Format("{0:F2}",heji.hejiSum ), this.Lab1.Font, Brushes.Black, this.Grid.Left + heji.Rec.X, 3, strfmt);            }                  }        //計算總和        public void sumdata()        {                       DataGridViewRowCollection rows = this.Grid.Rows;            foreach (HeJiMessage heji in li)            {                heji.hejiSum = 0;                             foreach (DataGridViewRow row in rows)                {                    heji.hejiSum += Convert.ToDecimal(row.Cells[heji.hejiName].Value);                                   }            }                      //重畫            this.Lab1.Invalidate();        }    }    //合計需要的資訊,列名,列索引,總和,位置資訊    public class HeJiMessage    {       public  string hejiName = null;       public int CellNumber = 0;       public decimal hejiSum = 0;       public Rectangle Rec;       public HeJiMessage() { }        /// <summary>        /// 初始化        /// </summary>        /// <param name="name">列名</param>        /// <param name="number">列索引</param>        /// <param name="rec">位置資訊</param>       public HeJiMessage(string name, int number,Rectangle rec)        {            hejiName = name;            CellNumber = number;            Rec = rec;        }    }#endregion

 

C#DataGridView合計處理

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.