Initialize DataRow in C #

Source: Internet
Author: User

 

In programs, DataRow is often used to save some data and complete the value transfer operation. In this case, some DataRow objects are formed, but they are organized using List or Datable. So how to initialize this DataRow? We know that DataRow is a able component, and on MSDN we can see that the constructor of DataRow is of the protected type. It can only be constructed internally and instantiated independently, dataRow row = new DataRow () is not allowed. DataRow can be instantiated in the following two ways.

 

1. The table of an existing DataTable object is known. Of course, the structure of this object already exists, as shown below:

 

 

DataRow row = table. NewRow ();

DataRow row = table. NewRow ();

In this way, a DataRow object with the same structure as a known table is instantiated, and columns can be assigned values.

 

2. If you do not have a known able, but want to save it in a self-written structure, you must first instantiate a DataTable that meets your needs, and then instantiate it, for example, 1.

 

 

Static DataTable dt = null;

Private static DataTable Dt

{

Get

{

If (dt = null)

{

Dt = new DataTable ();

 

Dt. Columns. Add (column name 1 );

 

Dt. Columns. Add (column name 2 );

 

Dt. Columns. Add (column name 3 );

}

Return dt;

}

}

Static DataTable dt = null;

Private static DataTable Dt

{

Get

{

If (dt = null)

{

Dt = new DataTable ();

 

Dt. Columns. Add (column name 1 );

 

Dt. Columns. Add (column name 2 );

 

Dt. Columns. Add (column name 3 );

}

Return dt;

}

}

Instantiate DataRow,

 

 

DataRow row = Dt. NewRow ();

DataRow row = Dt. NewRow ();

 

Then, if this row is referenced elsewhere, the value can be

 

 

Object value = row [column name 1]

Object value = row [column name 1]

In this way, we can save some data in the program, instead of having to write a Model class to save it, avoiding the inconvenience of referencing it elsewhere.

 

From Poplar

Related Article

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.