Three methods for adding columns and rows in DataTable and three methods for datatable

Source: Internet
Author: User

Three methods for adding columns and rows in DataTable and three methods for datatable

 

# Region Method 1: DataTable tblDatas = new DataTable ("Datas"); DataColumn dc = null; dc = tblDatas. columns. add ("ID", Type. getType ("System. int32 "); dc. autoIncrement = true; // automatically add dc. autoIncrementSeed = 1; // the start is 1 dc. autoIncrementStep = 1; // The step size is 1 dc. allowDBNull = false; dc = tblDatas. columns. add ("Product", Type. getType ("System. string "); dc = tblDatas. columns. add ("Version", Type. getType ("System. string "); dc = tblDatas. columns. add ("Description", Type. getType ("System. string "); DataRow newRow; newRow = tblDatas. newRow (); newRow ["Product"] = "this is the value of the cell"; newRow ["Version"] = "2.0 "; newRow ["Description"] = "this is the cell value"; tblDatas. rows. add (newRow); newRow = tblDatas. newRow (); newRow ["Product"] = "this is the value of the cell"; newRow ["Version"] = "3.0 "; newRow ["Description"] = "this is the cell value"; tblDatas. rows. add (newRow); # endregion
# Region Method 2: DataTable tblDatas = new DataTable ("Datas"); tblDatas. columns. add ("ID", Type. getType ("System. int32 "); tblDatas. columns [0]. autoIncrement = true; tblDatas. columns [0]. autoIncrementSeed = 1; tblDatas. columns [0]. autoIncrementStep = 1; tblDatas. columns. add ("Product", Type. getType ("System. string "); tblDatas. columns. add ("Version", Type. getType ("System. string "); tblDatas. columns. add ("Description", Type. getType ("System. string "); tblDatas. rows. add (newobject [] {null, "a", "B", "c"}); tblDatas. rows. add (newobject [] {null, "a", "B", "c"}); tblDatas. rows. add (newobject [] {null, "a", "B", "c"}); tblDatas. rows. add (newobject [] {null, "a", "B", "c"}); tblDatas. rows. add (newobject [] {null, "a", "B", "c"}); # endregion
# Region method 3: DataTable table = new DataTable (); // create the first column of the table DataColumn priceColumn = new DataColumn (); priceColumn. dataType = System. type. getType ("System. decimal "); // the Data Type priceColumn of this column. columnName = "price"; // The column name must be priceColumn. defaultValue = 50; // default value for this column // create the second column of the table DataColumn taxColumn = new DataColumn (); taxColumn. dataType = System. type. getType ("System. decimal "); taxColumn. columnName = "tax"; // column name taxColumn. expression = "price * 0.0862"; // sets the Expression for this column, used to calculate the value in the column or create an aggregate column // create the third column DataColumn totalColumn = new DataColumn () of the table (); totalColumn. dataType = System. type. getType ("System. decimal "); totalColumn. columnName = "total"; totalColumn. expression = "price + tax"; // Expression of this column, which is the value of the first and second columns and // Add all columns to the table. columns. add (priceColumn); table. columns. add (taxColumn); table. columns. add (totalColumn); // create a row DataRow row = table. newRow (); table. rows. add (row); // Add this row to the table // put the table in the view DataView view = new DataView (table); // bind it to the DataGrid dg. dataSource = view; dg. dataBind (); # endregion
DataTable table = SMRSCls. reportCenter. listCountSum (Keyword); table. columns. add ("PriceSum", Type. getType ("System. single "); // Add one more int RowsCount = table to the table. rows. count; for (int j = 0; j <RowsCount; j ++) // Add the corresponding value to the column {int CustomerID = Convert. toInt32 (table. rows [j] ["mermerid"]. toString (); int ProjectID = Convert. toInt32 (table. rows [j] ["ProjectID"]. toString (); int Account = SMRSCls. reportCenter. accountPrice (CustomerID, ProjectID); table. rows [j] ["PriceSum"] = Account ;}
DataTable dt = new DataTable (); dt. columns. add ("Name1", typeof (int); dt. columns. add ("Name2", typeof (object); dt. columns. add ("Name3", typeof (object); for (int I = 0; I <200; I ++) {dt. rows. add (I, "payment no." + Convert. toString (I), "color number" + Convert. toString (I);} gridControl1.DataSource = dt;
C # dynamically operate DataTable (adding rows, columns, querying rows, columns, etc)
Public void CreateTable () {// create a table DataTable dt = new DataTable (); // 1. Add a column dt. columns. add ("Name", typeof (string); // the data type is text // 2. Add the column DataColumn age = new DataColumn ("Age ", typeof (Int32); // the data type is Integer DataColumn Time = new DataColumn ("Time", typeof (DateTime); // the data type is Time dt. columns. add (age); dt. columns. add (Time); // 1. Add an empty row DataRow dr1 = dt. newRow (); dt. rows. add (dr1); // 2. Add a blank line dt. rows. add (); // 3. Add the data row DataRow dr2 = dt. newRow (); dr2 [0] = "Zhang San"; // assign a value to dr2 [1] = 23 through the index; dr2 ["Time"] = DateTime. now; // assign a value to dt by name. rows. add (dr2); // 4. Add dt through the row framework. rows. add ("Li Si", 25, DateTime. now); // Add the data sequence of your parameters to correspond to the column direction in dt}
Method 2: Add a new column for an existing DateTable. Its value can be set as the default value or the column cannot be blank. Public void CreateTable (DataTable vTable) {// Add a new column DataColumn dc1 = new DataColumn ("Tol", typeof (string) for an existing able; vTable. columns. add (dc1); // Add a new column with the default value: DataColumn dc2 = new DataColumn ("Sex", typeof (string); dc2.DefaultValue = "male "; dc2.AllowDBNull = false; // this parameter is used in the initial table. When adding columns to an existing table, vTable is not used. columns. add (dc2 );}
Method 3: filter the data in the able. Use the Select () method to save the selected results to DataRow [] drArr. You can also save the data as a new DataTable public void SelectRowDataTable () {DataTable dt = new DataTable (); // assume that dt is the result queried by "SELECT C1, C2, C3 FROM T1" for (int I = 0; I <dt. rows. count; I ++) {if (dt. rows [I] ["C1"]. toString () = "abc") // query condition {// perform Operation} // However, this method is recommended once or twice. If you use more, you will get tired. Is there any better way? Dt. select (). The preceding operation can be changed to the following: DataRow [] drArr = dt. select ("C1 = 'abc'"); // query (if the Select clause is unconditional, all data is queried) // you can perform the following operations: DataRow [] drArr1 = dt. select ("C1 LIKE 'abc % '"); // fuzzy query (if multiple criteria are selected, add and or) DataRow [] drArr2 = dt. select ("'abc' LIKE C1 + '%'", "C2 DESC"); // another fuzzy query method DataRow [] drArr3 = dt. select ("C1 = 'abc'", "C2 DESC"); // sorting // The problem arises again. How can I assign a value to the new DataRow? You may think of: DataTable dtNew1 = dt. clone (); for (int I = 0; I <drArr. length; I ++) {dtNew1.Rows. add (drArr [I]);} // However, an error occurs in the program, saying that the DataRow belongs to another DataTable. How can this problem be solved? It's easy, so we can solve the problem: DataTable dtNew2 = dt. clone (); for (int I = 0; I <drArr. length; I ++) {dtNew2.ImportRow (drArr [I]); // ImportRow is a copy }}
Method 4: filter the specified fields in the able and save them as the public void SelectColumnDataTable (dt able dt) {// filter the specified fields in the DataTable and save them as the new DataTable dtNew = dt. defaultView. toTable (false, new string [] {"column name", "column name", "column name"}); // These column names must exist in dt; otherwise, an error is reported}
Method 5: sort able (sort) public void SortDataTable (DataTable dt) {dt. defaultView. sort = "id desc"; // reset the Sort DataTable dtNew = dt. defaultView. toTable (); // save it in a new table}

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.