C#DataGridView合计处理

网上查了一些关于合计的代码 ,但发现大都都不尽人意,就自己再根据资料改了一下。

#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

 

你可能感兴趣的:(datagridview)