) C # create a able,

Source: Internet
Author: User

(Conversion) C # create a able,

1 Asp.net DataTable add columns and rows method 2 Method 1: 3 4 DataTable tblDatas = new DataTable ("Datas"); 5 DataColumn dc = null; 6 dc = tblDatas. columns. add ("ID", Type. getType ("System. int32 "); 7 dc. autoIncrement = true; // automatically add 8 dc. autoIncrementSeed = 1; // the start is 1 9 dc. autoIncrementStep = 1; // The step size is 110 dc. allowDBNull = false; // 11 12 dc = tblDatas. columns. add ("Product", Type. getType ("System. string "); 13 dc = tblDatas. columns. add ("Version", Type. getType ("System. string "); 14 dc = tblDatas. columns. add ("Description", Type. getType ("System. string "); 15 16 DataRow newRow; 17 newRow = tblDatas. newRow (); 18 newRow ["Product"] = "westward journey"; 19 newRow ["Version"] = "2.0 "; 20 newRow ["Description"] = "I like"; 21 tblDatas. rows. add (newRow); 22 23 newRow = tblDatas. newRow (); 24 newRow ["Product"] = "Fantastic westward journey"; 25 newRow ["Version"] = "3.0 "; 26 newRow ["Description"] = "more naive than big talk"; 27 tblDatas. rows. add (newRow); 28 29 Method 2: 30 31 DataTable tblDatas = new DataTable ("Datas"); 32 tblDatas. columns. add ("ID", Type. getType ("System. int32 "); 33 tblDatas. columns [0]. autoIncrement = true; 34 tblDatas. columns [0]. autoIncrementSeed = 1; 35 tblDatas. columns [0]. autoIncrementStep = 1; 36 37 tblDatas. columns. add ("Product", Type. getType ("System. string "); 38 tblDatas. columns. add ("Version", Type. getType ("System. string "); 39 tblDatas. columns. add ("Description", Type. getType ("System. string "); 40 41 tblDatas. rows. add (new object [] {null, "a", "B", "c"}); 42 tblDatas. rows. add (new object [] {null, "a", "B", "c"}); 43 tblDatas. rows. add (new object [] {null, "a", "B", "c"}); 44 tblDatas. rows. add (new object [] {null, "a", "B", "c"}); 45 tblDatas. rows. add (new object [] {null, "a", "B", "c"}); 46 47 method 3: 48 DataTable table = new DataTable (); 49 50 // 51 DataColumn priceColumn = new DataColumn (); 52 // the Data Type of the column 53 priceColumn. dataType = System. type. getType ("System. decimal "); 54 // The column name must be 55 priceColumn. columnName = "price"; 56 // The default value of this column is 57 priceColumn. defaultValue = 50; 58 59 // create the second column of the table 60 DataColumn taxColumn = new DataColumn (); 61 taxColumn. dataType = System. type. getType ("System. decimal "); 62 // column name 63 taxColumn. columnName = "tax"; 64 // sets the column expression to calculate the value in the column or create an aggregate column 65 taxColumn. expression = "price * 0.0862"; 66 // Create third column.67 DataColumn totalColumn = new DataColumn (); 68 totalColumn. dataType = System. type. getType ("System. decimal "); 69 totalColumn. columnName = "total"; 70 // expression of this column. The values are the values of the first and second columns and 71 totalColumn. expression = "price + tax"; 72 73 // Add all columns to table 74 table. columns. add (priceColumn); 75 table. columns. add (taxColumn); 76 table. columns. add (totalColumn); 77 78 // create a row 79 DataRow row = table. newRow (); 80 // Add this row to table 81 table. rows. add (row); 82 83 // put the table in the attempt 84 DataView view = new DataView (table); 85 dg. dataSource = view; 86 87 dg. dataBind ();

 

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.