小弟DataTable用的少!
现在需要用DataTable往里面添加东西不从数据库里加
而是自己手动加。该怎么写啊··各位
现在需要用DataTable往里面添加东西不从数据库里加
而是自己手动加。该怎么写啊··各位
解决方案 »
- 程序里的关键字在一些交换连接平台里出现乱码!!!请狠人进来看看,在线等!!
- 高分求高手解决!!!!!!!!!!!!!!~~~~~
- asp.net2.0 后台输出脚本
- 如何调用动态添加的控件
- 【求助】文本过滤出错???
- asp.net到点自动结贴,系统怎么实现?
- 如何最快的把Ip转化成32位整数
- 在DataGrid中在最后一页中删除最后一条记录出错
- 高分询问,asp.net中当数据库查询返回一条记录时,用何种方法显示到网页上最好?
- 高手们麻烦给我看一下下面的代码,我急啊!!!!谢谢了.这个一个网站的注册页面.关键代码;
- css如何实现这种效果?
- .net后台调用exe时为何出现拒绝访问? 急急急!!!!
//增加列
for (int i = 0; i < Dt.Rows.Count; i++)
{
Dt.Rows[i]["列名"] = 值;
}
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"] = "大话西游";
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);
DataColumn col=new DataColumn("col1",typeof(int));//定义一个列
table.Columns.Add(col);//将列添加到Table
DataRow dr=table.NewRow();//新建一数据行dr["col1"]="test";//为行赋值
table.Rows.Add(dr);//将行添加到Table手工写的,有错自己改一下,大概是这样子了
DataTable table = new DataTable("TableName");
table.Columns.Add("ID",typeof(string));
table.Columns.Add("Count", typeof(int)); for (int i = 0; i <= 7; i++)
{
DataRow row = new DataRow();
row["ID"] = "xxx";
row["Count"] = i;
table.Rows.Add(row);
}
DataTable table = new DataTable("myTable");
// create two columns , also set column data type and name
DataColumn col1=new DataColumn("ID",Type.GetType("System.Int32"));
DataColumn col2=new DataColumn("Name",Type.GetType("System.String"));
// add the columns above into tableColumns collection
table.Columns.Add(col1);
table.Columns.Add(col2);
// Add data to this table
for(int i =0;i<10;i++)
{
// create DataRow instance
DataRow row = table.NewRow();
row[0] = i; // you can alse write like row["ID"]
row[1]= "Name-"+i.ToString(); // like above,you can write like row["Name"]
// add this newly created row into Rows Collection of table, then there will be one new record/
table.Rows.Add(row);
}
DataTable dataTable = new DataTable("Products");
DataColumn pID=new DataColumn("ID",Type.GetType("System.Int32"));
DataColumn pName = new DataColumn("ProductName",Type.GetType("System.String"));
DataColumn pPrice = new DataColumn("ProductPrice",Type.GetType("System.Double")); dataTable.Columns.Add(pID);
dataTable.Columns.Add(pName);
dataTable.Columns.Add(pPrice);
dataTable.Constraints.Add("ProductsPrimaryKey", dataTable.Columns["ID"], true); DataRow row1 = dataTable.NewRow();
row1["ID"] = 1;
row1["ProductName"] = "apple";
row1["ProductPrice"] = "1.5";
dataTable.Rows.Add(row1); DataRow row2 = dataTable.NewRow();
row2["ID"] = 2;
row2["ProductName"] = "orange";
row2["ProductPrice"] = "2.0";
dataTable.Rows.Add(row2);