Home > Net >  C # is not binding datagridview control summary by using label controls
C # is not binding datagridview control summary by using label controls

Time:10-04

Beginner, write the datagridview control label control display in the summary below datagridview control method


///& lt; Summary>
///add summary laleb controls;
///& lt;/summary>
///& lt; Param name="temporary table to collect the column" & gt;
///& lt; Param name="to collect datagridview control & gt;"
///& lt; Param name="in the form" & gt;
Public void labeladd (DataTable field, DataGridView DGV, Form FM)
{
Int x=DGV. Location. X;//get the X coordinate DataGridView control;
Int y=DGV. Location. Y;//get the DataGridView control Y coordinates;
Int h=DGV. Size. Height;//get the height of the DataGridView control;

//to add the total control
Label lblsum=new Label ();//instantiate the Label control;
Lblsum. Name="lblsum";//set name for the LBL controls;
Lblsum. Text="combined";//set the text value;
Lblsum. TextAlign=ContentAlignment. MiddleRight;//set the right alignment;
Lblsum. The Font=new Font (lblsum. The Font. The Name, 9, FontStyle. Bold);//set the font, size, bold;
Lblsum. AutoSize=false;//not allowed to control the custom size;
Lblsum. Size=new Size (72, 23).//define size
Lblsum. Location=new Point (x, y + h + 3);//set position;
FM. Controls. The Add (lblsum);//add controls;

For (int I=0; I & lt; Field. The Rows. Count; I++)//cycle to summary the column array
{
Int lbly=y + h + 3;//set the label aggregation controls the initial Y coordinates;
Int LBLX=x + DGV. RowHeadersWidth;//set the label aggregation control initial X coordinates (DGV. RowHeadersWidth line width);
Label LBL=new Label ();//instantiate the Label control;
LBL. Name="LBL" + field. Rows [I] [0]. The ToString ();//set the control name to LBL + need to summary the column;
LBL. Text=LBL. Name. The ToString ();//set the initial text value=Name;
LBL. TextAlign=ContentAlignment. MiddleRight;//set the right alignment;
LBL. The Font=new Font (lblsum. The Font. The Name, 9, FontStyle. Bold);//set the font, size, bold;
LBL. AutoSize=false;//not allowed to control the custom size;
LBL. Size=new Size (72, 23).//define initial size
LBL. Location=new Point (150, y + h + 2);//initialize the first position;
FM. Controls. The Add (LBL);//add controls;
For (int j=0; J & lt; DGV. Columns. The Count; J++)
{
If (DGV. Columns [j]. Journal of Visible==true)//to determine whether a j column displays;
{
If (DGV. Columns [j]. Name the ToString ()==field. The Rows [I] [0]. The ToString ())//if the column Name is the same as the summary field
{
FM. Controls [" LBL "+ field. Rows [I] [0]. The ToString ()]. Location=new Point (LBLX lbly);//setting position
FM. Controls [" LBL "+ field. Rows [I] [0]. The ToString ()]. Size=new Size (DGV. Columns [j]. Journal of Width, 23).//set the summary label control width
}
The else
{
LBLX +=DGV. Columns [j]. Width;//
}
}
}
}
}

///& lt; Summary>
///datagridview control summary method;
///& lt;/summary>
///& lt; Param name="to collect field & gt;"
///& lt; Param name="to collect datagridview control & gt;"
Public void sumdata (DataTable field, DataGridView DetailDataGrid, Form FM)
{
For (int I=0; I & lt; Field. The Rows. Count; I++)
{
Field. The Rows [I] [1]=0;//initialize the amount of 0
}

For (int I=0; I & lt; DetailDataGrid. Rows. Count; I++)
{
For (int j=0; J & lt; Field. The Rows. Count; J++)
{
Field. Rows [j]. [1]=decimal Parse (field. The Rows [j] [1]. The ToString ()) + a decimal. Parse (DetailDataGrid. Rows [I] Cells [field. The Rows [j] [0]. The ToString ()]. The Value. The ToString ());//summary column accumulation
FM. Controls [" LBL "+ field. Rows [j] [0]. The ToString (), ToString ()]. The Text=field. The Rows [j] [1]. The ToString ();//set the text property controls;
}
}

}

Private void FmSalesInvoice_Load (object sender, EventArgs e)//form loads defined when compiling a temporary table;
{
The DataTable field=new DataTable (" tbfieldSum ");//instantiate to summary the temporary table;
Field. The Columns. The Add (" fieldname ", typeof (String));//add columns;
Field. The Columns. The Add (" fieldsum typeof (decimal));//add summary columns;
Field. The Rows. The Add (" sum1 ", 0);//add the datagridview control to collect column names as a temporary table rows, initialize the aggregate amount of 0;
Field. The Rows. The Add (" sum2, 0);//add the datagridview control to collect column names as a temporary table rows, initialize the aggregate amount of 0;
Field. The Rows. The Add (" sum3, 0);//add the datagridview control to collect column names as a temporary table rows, initialize the aggregate amount of 0;
Labeladd (field, datagridview1, this);//call adding subtotals laleb control method;
Sumdata (field, datagridview1, this);//call summary datagridview control method

}


Private void datagridview1_Scroll (object sender, ScrollEventArgs e)//scroll bar trigger events;
{
If (e.S crollOrientation==ScrollOrientation. HorizontalScroll)//determine whether the horizontal scroll bar;
{
For (int I=0; I & lt; Field. The Rows. Count; I++)
{
Controls [" LBL "+ field Rows [I] [0]. The ToString ()], Left -=(e.N ewValue - e.O ldValue);//set with the scroll bar controls display;
}

}
}

CodePudding user response:

Fine, well, please tell your problem,,, continue to work hard to write good,,, have not you can ask,;

CodePudding user response:

Well well,,
  •  Tags:  
  • C#
  • Related