DataTable dt=new DataTable("cart");
DataColumn dc1=new DataColumn("prizename",Type.GetType("System.String"));
DataColumn dc2=new DataColumn("point",Type.GetType("System.Int16"));
DataColumn dc3=new DataColumn("number",Type.GetType("System.Int16"));
DataColumn dc4=new DataColumn("totalpoint",Type.GetType("System.Int64"));
DataColumn dc5=new DataColumn("prizeid",Type.GetType("System.String"));
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
dt.Columns.Add(dc4);
dt.Columns.Add(dc5);
//以上代码完成了DataTable的构架,但是里面是没有任何数据的
for(int i=0;i<10;i++)
{
DataRow dr=dt.NewRow();
dr["prizename"]="娃娃";
dr["point"]=10;
dr["number"]=1;
dr["totalpoint"]=10;
dr["prizeid"]="001";
dt.Rows.Add(dr);
}
//填充了10条相同的记录进去
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
有人会这么做
DataRow dr=new DataRow();
..
dt.Rows.Add(dr);
这样是不行的,因为一个DataRow必定是属于一个DataTable的,不能凭空建立,就好比一条记录一定是属于一个表一样
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
又有人这么做
DataRow dr=dt.NewRow();
dr["prizename"]="娃娃";
dr["point"]=10;
dr["number"]=1;
dr["totalpoint"]=10;
dr["prizeid"]="001";
for(int i=0;i<10;i++)
{
dt.Rows.Add(dr);
}
这样做同样是错误的,因为DataTable已经有这条DataRow了,就好比一个表中不可能存在2条一样的记录,必须每次NewRow()
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)