I. About this article
The datatablehelper class in this article includes four function operations for datatable, which are
1) public static datatable gettestdatatable ()
This is a test function that generates a datatable with content.
2) public static string printdatatable (datatable DT)
This function prints all the content in a datatable to the console.
3) Public static datatable getanotherdatatable (datatable DT)
This function creates a copy of The datatable that is exactly the same as the original datatable.
4) Public static datatable sort (datatable DT, string rule)
This function uses the rules specified by rule to return a new sorted datatable.
Ii. datatablehelper class code
You need to add using system. Data;
/// <Summary> /// datatable tool class /// </Summary> class datatablehelper {/// <summary> /// generate a datatable for testing /// </Summary> // <returns> datatable for testing </returns> Public static datatable gettestdatatable () {datatable dt = new datatable ("datatable4test"); DT. columns. add ("ID"); DT. columns. add ("name"); DT. columns. add ("data"); DT. rows. add ("0", "tsybius", "20140509"); DT. rows. add ("1", "Galatea", "20140723"); dt. Rows. add ("2", "Gnaeus", "20130314"); DT. rows. add ("3", "Titus", "20130104"); DT. rows. add ("4", "Publius", "20151111"); Return DT ;} /// <summary> /// print the datatable content to the console /// </Summary> /// <Param name = "DT"> </param> /// <returns> </returns> Public static string printdatatable (datatable DT) {stringbuilder sb = new stringbuilder (); // 1. print the datatable name sb. append ("tablename:" + dt. tablename + "\ n"); // 2. print Column header for (INT I = 0; I <DT. columns. count; I ++) {sb. append (DT. columns [I]. columnname + "\ t");} sb. append ('\ n'); // 3. print each column for (INT I = 0; I <DT. rows. count; I ++) {for (Int J = 0; j <DT. columns. count; j ++) {sb. append (DT. rows [I] [J]. tostring () + "\ t");} sb. append ('\ n');} sb. append ("output complete! "); Return sb. tostring ();} /// <summary> /// generate a datatable exactly the same as the original datatable /// </Summary> /// <Param name = "DT"> </param>/ // <returns> </returns> Public static datatable getanotherdatatable (datatable DT) {datatable dtanother = DT. clone (); For (INT I = 0; I <DT. rows. count; I ++) {dtanother. rows. add (DT. rows [I]. itemarray);} return dtanother ;} /// <summary> /// sort the able /// </Summary> /// <Param name = "DT"> data source to be sorted </param> // /<Param name = "rule"> sort item of dataview </param> // <returns> A sorted data source </returns> Public static datatable sort (datatable DT, string rule) {dataview DV = DT. defaultview; DV. sort = rule; return DV. totable ();}}
Ii. Main function call example
Class program {static void main (string [] ARGs) {// 1. generate a datatable for testing and output its content datatable dt1 = datatablehelper. gettestdatatable (); // obtain the test datatable string a = datatablehelper. printdatatable (dt1); // outputs the console of the datatable content. writeline (a); console. writeline ("----"); // 2. generate a datatable dt2 = datatablehelper with the same structure as DT. getanotherdatatable (dt1); dt2.rows [0]. delete (); console. writeline ("dt1 rows:" + dt1.rows. count); // 5 console. writeline ("dt2 rows:" + dt2.rows. count); // 4 console. writeline ("----"); // 3. sort by dt1 (1) datatable dt3 = datatablehelper. sort (dt1, "id DESC"); string B = datatablehelper. printdatatable (dt3); console. writeline (B); console. writeline ("----"); // 4. sort by dt1 (2) datatable dt4 = datatablehelper. sort (dt1, "Name ASC, data DESC"); string c = datatablehelper. printdatatable (dt4); console. writeline (c); console. readline ();}}
Iii. Running Effect
End