C # Add sequence numbers to datatable, C # Add aggregate and subtotal to datatable

Source: Internet
Author: User
/// <Summary>
/// Add the sequence number to the datatable
/// </Summary>
/// <Param name = "DT"> </param>
/// <Param name = "colxuhao"> </param>
Public static void dbdatatableaddxuhao (datatable DT, string colxuhao ){
For (INT I = 0; I <DT. Rows. Count; I ++ ){
DT. Rows [I] [colxuhao] = I + 1;
}
}
/// <Summary>
/// Add the total row at the end of the datatable. The last two parameters set the range of rows to be total. datatablesummulticol (DT, 3rd, 5) // The total rows are, 4, 5, and 6.
/// </Summary>
/// <Param name = "DT"> </param>
/// <Param name = "ncolheji"> Columns whose 'Total' fields need to be displayed </param>
/// <Param name = "nmincolheji"> Columns starting from the total </param>
/// <Param name = "nmaxcolheji"> end of the total column.-1 indicates all </param>
Public static datarow dbdatatablesumrowswithcolrange (datatable DT, int ncolheji, int nmincolheji, int nmaxcolheji ){
If (nmaxcolheji =-1) nmaxcolheji = DT. Columns. Count-1;
If (nmaxcolheji <nmincolheji) custerror ("total columns, the end column is larger than the start column! ");
Int [] IX = new int [nmaxcolheji-nmincolheji + 1];
For (INT I = 0; I <IX. length; I ++ ){
IX [I] = I + nmincolheji;
}
Return dbdatatablesumrowswithcollist (DT, ncolheji, IX );
// Int [] I = nmaxcolhe
}
/// <Summary>
/// Add the total row at the end of the datatable. The last parameter sets the columns to be aggregated. datatablesummulticol (DT, 1, {3rd, 8}) // The total rows are, 7, and 9.
/// </Summary>
/// <Param name = "DT"> </param>
/// <Param name = "ncolheji"> Columns whose 'Total' fields need to be displayed </param>
/// <Param name = "colsheji"> columns to be aggregated </param>
Public static datarow dbdatatablesumrowswithcollist (datatable DT, int ncolheji, int [] colsheji ){
Datarow DR = DT. newrow ();
Dr [ncolheji] = "Total ";
DT. Rows. Add (DR );
// Initialize the total Array
Decimal [] arrdec = new decimal [colsheji. Length];
For (INT I = 0; I <colsheji. length; I ++ ){
Arrdec [I] = decimal. zero;
}
// Total
For (INT I = 0; I <DT. Rows. Count-1; I ++ ){
For (Int J = 0; j <colsheji. length; j ++ ){
Int c = colsheji [J];
If (convert. isdbnull (Dt. Rows [I] [c]) continue;
Arrdec [J] + = tools. todec (Dt. Rows [I] [c] + "");
}
}
// Assign a value
For (INT I = 0; I <colsheji. length; I ++ ){
Int c = colsheji [I];
If (arrdec [I] = decimal. Zero) Dr [c] = dbnull. value;
Else Dr [c] = arrdec [I];
}
Return Dr;
}
/// <Summary>
/// Add the total row at the end of the datatable. The last parameter sets the columns to be total, tools. dbdatatablesumrowswithcollist (DT, "Car ID", new string [] {"personal output", "fuel-saving (yuan)", "ultra-oil (yuan )"});
/// </Summary>
/// <Param name = "DT"> </param>
/// <Param name = "ncolheji"> Columns whose 'Total' fields need to be displayed </param>
/// <Param name = "colsheji"> columns to be aggregated </param>
Public static datarow dbdatatablesumrowswithcollist (datatable DT, string scolheji, string [] colsheji ){
Datarow DR = DT. newrow ();
Dr [scolheji] = "Total ";
DT. Rows. Add (DR );
// Initialize the total Array
Decimal [] arrdec = new decimal [colsheji. Length];
For (INT I = 0; I <colsheji. length; I ++ ){
Arrdec [I] = decimal. zero;
}
// Total
For (INT I = 0; I <DT. Rows. Count-1; I ++ ){
For (Int J = 0; j <colsheji. length; j ++ ){
String cname = colsheji [J];
If (convert. isdbnull (Dt. Rows [I] [cname]) continue;
Arrdec [J] + = tools. todec (Dt. Rows [I] [cname] + "");
}
}
// Assign a value
For (INT I = 0; I <colsheji. length; I ++ ){
String cname = colsheji [I];
If (arrdec [I] = decimal. Zero) Dr [cname] = dbnull. value;
Else Dr [cname] = arrdec [I];
}
Return Dr;
}
/// <Summary>
/// Subtotal of categories with total rows,
/// </Summary>
/// <Param name = "DT"> </param>
/// <Param name = "ncolheji"> Columns whose 'Total' fields need to be displayed </param>
/// <Param name = "colsheji"> columns to be aggregated </param>
Public datarow dbdatatablesubsumrowswithcollist (datatable DT, string scolheji, string [] colsgroup, string [] colsheji ){
Datarow DR = DT. newrow ();
Dr [scolheji] = "Total ";
DT. Rows. Add (DR );
// Initialize the total Array
Decimal [] arrdec = new decimal [colsheji. Length];
For (INT I = 0; I <colsheji. length; I ++ ){
Arrdec [I] = decimal. zero;
}
// Total
For (INT I = 0; I <DT. Rows. Count-1; I ++ ){
For (Int J = 0; j <colsheji. length; j ++ ){
String cname = colsheji [J];
If (convert. isdbnull (Dt. Rows [I] [cname]) continue;
Arrdec [J] + = tools. todec (Dt. Rows [I] [cname] + "");
}
}
For (INT I = 0; I <colsheji. length; I ++ ){
String cname = colsheji [I];
If (arrdec [I] = decimal. Zero) Dr [cname] = dbnull. value;
Else Dr [cname] = arrdec [I];
}
If (Dt. Rows. Count <= 1) return Dr;
// Subtotal
String srate = "";
Int currsubsumcol = DT. Rows. Count-2;
Arraylist AR = new arraylist ();
For (INT I = DT. Rows. Count-2; I> = 0; I --){
String currrate = DT. Rows [I] ["Tax Rate (%)"] + "";
If (srate! = Currrate ){
If (I! = DT. Rows. Count-2 ){
Dr = DT. newrow ();
Dr [scolheji] = "subtotal ";
For (Int J = 0; j <colsheji. length; j ++ ){
String cname = colsheji [J];
If (arrdec [J] = decimal. Zero) Dr [cname] = dbnull. value;
Else Dr [cname] = arrdec [J];
}
DT. Rows. insertat (DR, currsubsumcol + 1 );
}
Currsubsumcol = I;
Srate = currrate;
For (Int J = 0; j <colsheji. length; j ++) {// return to zero
Arrdec [J] = decimal. zero;
}
}
For (Int J = 0; j <colsheji. length; j ++ ){
String cname = colsheji [J];
If (convert. isdbnull (Dt. Rows [I] [cname]) continue;
Arrdec [J] + = tools. todec (Dt. Rows [I] [cname] + "");
}
If (I = 0 ){
Dr = DT. newrow ();
Dr [scolheji] = "subtotal ";
For (Int J = 0; j <colsheji. length; j ++ ){
String cname = colsheji [J];
If (arrdec [J] = decimal. Zero) Dr [cname] = dbnull. value;
Else Dr [cname] = arrdec [J];
}
DT. Rows. insertat (DR, currsubsumcol + 1 );
}
}

Return Dr;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.