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