C # directly creates a DataTable and adds data to it (custom DataTable) to

Source: Internet
Author: User

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);
The above code completes the schema of the DataTable, but there is no data in it
for (int i=0;i<10;i++)
{
DataRow Dr=dt. NewRow ();
dr["Prizename"]= "Doll";
dr["Point"]=10;
dr["Number"]=1;
dr["Totalpoint"]=10;
dr["Prizeid"]= "001";
Dt. Rows.Add (DR);
}
10 identical records are filled in.

Somebody's going to do this.
DataRow dr=new DataRow ();
..
Dt. Rows.Add (DR);
This is not possible, because a DataRow must belong to a DataTable, can not be built out of thin air, like a record must belong to a table like

Someone else did it again.
DataRow Dr=dt. NewRow ();
dr["Prizename"]= "Doll";
dr["Point"]=10;
dr["Number"]=1;
dr["Totalpoint"]=10;
dr["Prizeid"]= "001";
for (int i=0;i<10;i++)
{
Dt. Rows.Add (DR);
}
This is also wrong, because the DataTable already has this DataRow, just like there is no 2 records in a table, must each newrow ()

C # directly creates a DataTable and adds data to it (custom DataTable) to

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.