C # DataTable usage

Source: Internet
Author: User


// Create a table
DataTable tDataTable = new DataTable ("test ");
// Create a column
DataColumn tDataCol = new DataColumn ("name", System. Type. GetType ("System. String "));
// Add a column
TDataTable. Columns. Add (tDataCol );
TDataCol = new DataColumn ("age", System. Type. GetType ("System. Int32 "));
// Add a column
TDataTable. Columns. Add (tDataCol );
// Add each row of data
DataRow tDataRow = tDataTable. NewRow ();
TDataRow ["name"] = "xiaoming ";
TDataRow ["age"] = 14;
TDataTable. Rows. Add (tDataRow );

DataRow tDataRowOne = tDataTable. NewRow ();
TDataRowOne ["name"] = "xiaoqiang ";
TDataRowOne ["age"] = 24;
TDataTable. Rows. Add (tDataRowOne );

// Traverse the Datatable
Foreach (DataRow t in tDataTable. Rows)
{
Object Name = t [0];
Object age = t [1];
}

// Delete data
DataRow [] tDelet = tDataTable. Select ("name = 'xiaoming '");
TDataTable. Rows. Remove (tDelet [0]);

 

 

 

Public DataTable GetAllDataTable (DataSet ds) {DataTable newDataTable = ds. Tables [0]. Clone (); // create a new table Clone to have a table schema.
Object [] objArray = new object [newDataTable. Columns. Count]; // defines the values of a row in the table in the array of objects with the same number of Columns as the table.
For (int I = 0; I <ds. tables. count; I ++) {for (int j = 0; j <ds. tables [I]. rows. count; j ++) {ds. tables [I]. rows [j]. itemArray. copyTo (objArray, 0); // stores the values of a row in the array.
NewDataTable. Rows. Add (objArray); // Add the array value to the new table. }}
Return newDataTable; // return the new table .}
// Configure //------------------------------------------------------------------------------------------------------------------------------

Merge two able data into one able // two DT data tables with the same structure. able DataTable1 = new able (); DataTable DataTable2 = new DataTable (); DataTable newDataTable = DataTable1.Clone ();
Object [] obj = new object [newDataTable. Columns. Count];
For (int I = 0; I <ableable1.rows. Count; I ++) {DataTable1.Rows [I]. ItemArray. CopyTo (obj, 0 );
NewDataTable. Rows. Add (obj );}

For (int I = 0; I <ableable2.rows. Count; I ++) {DataTable2.Rows [I]. ItemArray. CopyTo (obj, 0 );
NewDataTable. Rows. Add (obj );}
// Merge DT with different structures /// <summary> /// combine the two datatables with different columns into a new DataTable /// </summary> // <param name = "dt1"> Table 1 </param> // <param name = "dt2"> Table 2 </param> // <param name = "DTName"> New table name after merging </param> // <returns> </returns> private DataTable UniteDataTable (DataTable dt1, dataTable dt2, string DTName) {DataTable dt3 = dt1.Clone ();
For (int I = 0; I <dt2.Columns. Count; I ++) {dt3.Columns. Add (dt2.Columns [I]. ColumnName );}
Object [] obj = new object [dt3.Columns. Count];
For (int I = 0; I <dt1.Rows. Count; I ++) {dt1.Rows [I]. ItemArray. CopyTo (obj, 0 );
Dt3.Rows. Add (obj );}
If (dt1.Rows. count> = dt2.Rows. count) {for (int I = 0; I <dt2.Rows. count; I ++) {for (int j = 0; j <dt2.Columns. count; j ++) {dt3.Rows [I] [j + dt1.Columns. count] = dt2.Rows [I] [j]. toString () ;}} else {DataRow dr3;
For (int I = 0; I <dt2.Rows. Count-dt1.Rows. Count; I ++) {dr3 = dt3.NewRow ();
Dt3.Rows. Add (dr3 );}
For (int I = 0; I <dt2.Rows. count; I ++) {for (int j = 0; j <dt2.Columns. count; j ++) {dt3.Rows [I] [j + dt1.Columns. count] = dt2.Rows [I] [j]. toString ();}}}
Dt3.TableName = DTName; // sets the DT name.
Return dt3 ;}

 

 

 

 

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.