標籤:
網上查了一些關於合計的代碼 ,但發現大都都不盡人意,就自己再根據資料改了一下。
#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合計處理