C # to create a DataTable (reprint)

来源:https://www.cnblogs.com/xietianjiao/p/11213121.html
方法一: DataTable tblDatas = new DataTable("Datas"); DataColumn dc = null; dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32")); dc.AutoIncrement = true;//自动增加 dc.AutoIncrementSeed = 1;//起始为1 dc.AutoIncrementStep = 1;//步长为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"] = "Odyssey "; the newRow [" Version "] =" 2.0 "; newRow [" the Description "] =" I like "; tblDatas.Rows.Add(newRow); newRow = tblDatas.NewRow(); newRow["Product"] = "梦幻西游"; newRow["Version"] = "3.0"; newRow["Description"] = "比大话更幼稚"; tblDatas.Rows.Add(newRow); 方法二: 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(new object[]{null,"a","b","c"}); tblDatas.Rows.Add(new object[] { null, "a", "b", "c" }); tblDatas.Rows.Add(new object[] { null, "a", "b", "c" }); tblDatas.Rows.Add(new object[] { null, "a", "b", "c" }); tblDatas.Rows.Add(new object[] { null, "a", "b", "c" }); 方法三: DataTable table = new DataTable (); //创建table的第一列 DataColumn priceColumn = new DataColumn(); //该列的数据类型 priceColumn.DataType = System.Type.GetType("System.Decimal"); //该列得名称 priceColumn.ColumnName = "price"; //该列得默认值 priceColumn.50 = the DefaultValue; DataColumn taxColumn = new DataColumn (); // create a second column of table taxColumn.DataType = System. Type.GetType("System.Decimal"); //列名 taxColumn.ColumnName = "tax"; // Set the column we obtain the expression for calculating the values in the column or columns create the aggregate taxColumn.Expression = "*. price 0.0862"; // the Create column THIRD. the DataColumn totalColumn the DataColumn new new = (); totalColumn.DataType = System.Type.GetType ( "the System.Decimal"); totalColumn.ColumnName = "Total"; // the column expression, the value is obtained are and a second row worth and totalColumn.Expression = "+ Tax. price"; // add all of the columns to Table table.Columns.Add (priceColumn); table.Columns.Add (taxColumn); table.Columns. the Add (totalColumn); // create a line of the DataRow row = table.NewRow (); // add this line to the table in table.Rows.Add (row); // try to put the table in the DataView view = new DataView ( Table); dg.DataSource = View; dg.DataBind ();
来源:https://www.cnblogs.com/xietianjiao/p/11213121.html
方法一: DataTable tblDatas = new DataTable("Datas"); DataColumn dc = null; dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32")); dc.AutoIncrement = true;//自动增加 dc.AutoIncrementSeed = 1;//起始为1 dc.AutoIncrementStep = 1;//步长为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"] = "Odyssey "; the newRow [" Version "] =" 2.0 "; newRow["Description"] = "I like"; tblDatas.Rows.Add (newRow); newRow = tblDatas.NewRow(); newRow["Product"] = "梦幻西游"; newRow["Version"] = "3.0"; newRow["Description"] = "比大话更幼稚"; tblDatas.Rows.Add(newRow); 方法二: 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 (new new Object [] {null, "A", "B", "C"}); tblDatas.Rows.Add(new object[] { null, "a", "b", "c" }); tblDatas.Rows.Add (new new Object [] {null, "A", "B", "C "}); tblDatas.Rows.Add (new new Object [] {null," A "," B "," C "}); tblDatas.Rows.Add (new new Object [] {null," A "," B "," c "}); method three: the DataTable the DataTable table = new new (); // Create a table the first column DataColumn priceColumn = new DataColumn (); data type of the column // priceColumn.DataType = System.Type. the GetType ( "the System.Decimal"); // get the column name priceColumn.ColumnName = ". price"; // default value of the column to give priceColumn.DefaultValue = 50; // create a second column of table DataColumn taxColumn = new DataColumn ( ); taxColumn.DataType = System.Type.GetType("System.Decimal"); //列名 taxColumn.ColumnName = "Tax"; // Set the column we obtain the expression for calculating the value of the column or columns create the aggregate taxColumn.Expression = "*. Price 0.0862"; // the Create THIRD column. = New new totalColumn the DataColumn the DataColumn (); totalColumn.DataType = System.Type.GetType ( "the System.Decimal"); totalColumn.ColumnName = "Total"; // the column expression, the value is obtained is the first column and and the second column is worth totalColumn.Expression = "+ Tax. price"; // add all of the columns to Table table.Columns.Add (priceColumn); table.Columns.Add (taxColumn); table.Columns.Add (totalColumn ); // create a line of the DataRow row = table.NewRow (); // add this line to the table in table.Rows.Add (row); // try to put the table in the DataView view = new DataView (table) ; = View dg.DataSource; dg.DataBind ();
来源:https://www.cnblogs.com/xietianjiao/p/11213121.html
方法一: DataTable tblDatas = new DataTable("Datas"); DataColumn dc = null; dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32")); dc.AutoIncrement = true;//自动增加 dc.AutoIncrementSeed = 1;//起始为1 dc.AutoIncrementStep = 1;//步长为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"] = "Odyssey "; the newRow [" Version "] =" 2.0 "; newRow["Description"] = "I like"; tblDatas.Rows.Add (newRow); newRow = tblDatas.NewRow(); newRow["Product"] = "梦幻西游"; newRow["Version"] = "3.0"; newRow["Description"] = "比大话更幼稚"; tblDatas.Rows.Add(newRow); 方法二: 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 (new new Object [] {null, "A", "B", "C"}); tblDatas.Rows.Add(new object[] { null, "a", "b", "c" }); tblDatas.Rows.Add (new new Object [] {null, "A", "B", "C "}); tblDatas.Rows.Add (new new Object [] {null," A "," B "," C "}); tblDatas.Rows.Add (new new Object [] {null," A "," B "," c "}); method three: the DataTable the DataTable table = new new (); // Create a table the first column DataColumn priceColumn = new DataColumn (); data type of the column // priceColumn.DataType = System.Type. the GetType ( "the System.Decimal"); // get the column name priceColumn.ColumnName = ". price"; // default value of the column to give priceColumn.DefaultValue = 50; // create a second column of table DataColumn taxColumn = new DataColumn ( ); taxColumn.DataType = System.Type.GetType("System.Decimal"); //列名 = taxColumn.ColumnName "Tax"; // Set the column we obtain the expression for calculating the values in the column or columns create the aggregate taxColumn.Expression = "*. price 0.0862"; // the Create THIRD column. = New new totalColumn the DataColumn the DataColumn (); totalColumn.DataType = System.Type.GetType ( "the System.Decimal"); totalColumn.ColumnName = "Total"; // the column expression, the value is obtained is the first column and and the second column is worth totalColumn.Expression = "+ Tax. price"; // add all of the columns to Table table.Columns.Add (priceColumn); table.Columns.Add (taxColumn); table.Columns.Add (totalColumn ); // create a line of the DataRow row = table.NewRow (); // add this line to the table in table.Rows.Add (row); // try to put the table in the DataView view = new DataView (table) ; = View dg.DataSource; dg.DataBind ();

Guess you like

Origin www.cnblogs.com/hao-1234-1234/p/11763950.html