On the Internet to check some of the total code, but found that most of them are not satisfactory, they then changed according to the information.
#regionTotal//Invoke Method Example//Heji heji = null; //Heji = new Heji (LABEL1,DATAGRIDVIEW1); //Heji. Addheji ("ftransportoperator_id", 4); //Heji. Addheji ("floadingteam_id", 6); //Heji.sumdata (); Public classHeji {//two control text, table PrivateLabel Lab1; PrivateDataGridView Grid; //defines a private variable for a class, and two is a scroll barHScrollBar HS; VScrollBar vs; //Total list of informationListNewList(); //Initialize PublicHeji () {} PublicHeji (Label Lab, DataGridView grid) {LAB1=Lab; Grid=Grid; HS= ((HScrollBar) This. grid.controls[0]); VS= ((VScrollBar) This. grid.controls[1]); Hs. ValueChanged+=NewEventHandler (hs_valuechanged); Vs. ValueChanged+=NewEventHandler (vs_valuechanged); Lab1.paint+=NewPainteventhandler (Lab_paint); Grid.cellvaluechanged+=NewDatagridviewcelleventhandler (cellvaluechanged); } //scroll bar Delegate Event Public voidHs_valuechanged (Objectsender, EventArgs e) { This. Lab1.invalidate (); } Public voidVs_valuechanged (Objectsender, EventArgs e) { This. Lab1.invalidate (); } /// <summary> ///Table Value Change/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidCellvaluechanged (Objectsender, DataGridViewCellEventArgs e) {Sumdata (); } /// <summary> /////Add columns that need to be totaled/// </summary> /// <param name= "name" >Field name</param> /// <param name= "number" >Column Index</param> Public voidAddheji (stringNameintNumber ) {Rectangle rec= This. Grid.getcelldisplayrectangle (number,0,false); Li. ADD (Newhejimessage (name, Number,rec)); } /// <summary> ///Paint Delegate event, repaint/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidLab_paint (Objectsender, PaintEventArgs e) {datagridviewcolumncollection Columns= This. Grid.columns; intCount =Grid.Columns.Count; Graphics GRF=E.graphics; StringFormat strfmt=NewStringFormat (); Strfmt. Alignment=Stringalignment.center; //determine which lines are displayed, and use rows to get rectangle location information foreach(Hejimessage Hejiinchli) {Rectangle rec= This. Grid.getcelldisplayrectangle (Heji. Cellnumber,0,false); for(inti =0; i < Grid.Rows.Count; i++) { if(Grid.rows[i]. Displayed) {Rec= This. Grid.getcelldisplayrectangle (Heji. Cellnumber, I,false); Heji. Rec=Rec; Console.WriteLine ("Display behavior:"+i.tostring ()); Break; }} GRF. DrawString (string. Format ("{0:F2}", Heji.hejisum), This. Lab1.font, Brushes.black, This. Grid.left + Heji. Rec.x,3, STRFMT); } } //Calculate sum Public voidSumdata () {datagridviewrowcollection rows= This. Grid.rows; foreach(Hejimessage Hejiinchli) {heji.hejisum=0; foreach(DataGridViewRow rowinchrows) {Heji.hejisum+=convert.todecimal (row. Cells[heji.hejiname]. Value); } } //re-painting This. Lab1.invalidate (); } } //Aggregate required information, column name, column index, SUM, location information Public classHejimessage { Public stringHejiname =NULL; Public intCellnumber =0; Public decimalHejisum =0; PublicRectangle Rec; Publichejimessage () {}/// <summary> ///Initialize/// </summary> /// <param name= "name" >Column Name</param> /// <param name= "number" >Column Index</param> /// <param name= "Rec" >Location Information</param> PublicHejimessage (stringNameintNumber,rectangle Rec) {Hejiname=name; Cellnumber=Number ; Rec=Rec; } }#endregion
C#datagridview Total Processing