首页 > 代码库 > C#创建datatable (转)
C#创建datatable (转)
C#创建datatable 方法一: DataTable tblDatas = new DataTable("Datas");DataColumn dc = null;dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));dc.AutoIncrement = true;//自动增加dc.AutoIncrementSeed = 1;//起始为1dc.AutoIncrementStep = 1;//步长为1dc.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"] = "大话西游";newRow["Version"] = "2.0";newRow["Description"] = "我很喜欢";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.DefaultValue = 50; // 创建table的第二列DataColumn taxColumn = new DataColumn();taxColumn.DataType = System.Type.GetType("System.Decimal");//列名taxColumn.ColumnName = "tax";//设置该列得表达式,用于计算列中的值或创建聚合列taxColumn.Expression = "price * 0.0862";// Create third column.DataColumn totalColumn = new DataColumn();totalColumn.DataType = System.Type.GetType("System.Decimal");totalColumn.ColumnName = "total";//该列的表达式,值是得到的是第一列和第二列值得和totalColumn.Expression = "price + tax"; // 将所有的列添加到table上table.Columns.Add(priceColumn);table.Columns.Add(taxColumn);table.Columns.Add(totalColumn); //创建一行DataRow row = table.NewRow();//将此行添加到table中table.Rows.Add(row); //将table放在试图中DataView view = new DataView(table);dg.DataSource = view; dg.DataBind();
C#创建datatable (转)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。