A common method for assigning values to another able

Source: Internet
Author: User
DataView view = new DataView ();
View. Table= DataTableA;
View. RowFilter = "itemType = 'book'"; // itemType is a field in ableablea.
DataTableB = view. ToTable ();
Or:
DataRow [] rows = ableablea. Select ("itemType = 'book '");
DataTableB = DataTableA. Clone ();
Foreach (DataRow row in rows)
{
DataTableB. ImportRow (row );
}

Or

/// Execute the query in the DataTable to return the new DataTable
/// </Summary>
/// <Param name = "dt"> source data DataTable </param>
/// <Param name = "condition"> query condition </param>
/// <Returns> </returns>
Private DataTable GetNewDataTable (DataTable dt, string condition, string sortstr)
{
DataTable newdt = new DataTable ();
Newdt = dt. Clone ();
DataRow [] dr = dt. Select (condition, sortstr );
For (int I = 0; I <dr. Length; I ++)
      {
Newdt. ImportRow (DataRow) dr [I]);
      }
Return newdt; // The Returned query result.

}

 

 

Or column by column

Public static int TableDataExchange (DataSet ds, string tableName, DataTable sourceDT)
        {
For (int I = 0; I <sourceDT. Rows. Count; I ++)
             {
DataRow drNew = ds. Tables [tableName]. NewRow ();
Foreach (DataColumn dc in sourceDT. Columns)
                 {
DrNew [dc. ColumnName] = sourceDT. Rows [I] [dc. ColumnName];
                 }
Ds. Tables [tableName]. Rows. Add (drNew );
             }
Ds. Tables [tableName]. AcceptChanges ();
DataTable dt = ds. Tables [tableName];
Return ds. Tables [tableName]. Rows. Count;
             
        }

 

 

Or

 

DataSet objects support ADO. the core object of the NET disconnected and distributed data solution, which is widely used. we often need to use the data, such as getting data from a able, copying data from another ABE, or data from DataRow. However, only the replication of DataSet and DataTable supports deep replication, that is to say, the structure of the element can be copied, and the data of the element can be copied. The DatatDataRow has no relevant replication method. The following describes the replication of these data elements.

DataTable sourceTable;
DataTable objectTable;
DatatDataRow sourceRow;
DatatDataRow objectRow;
DataRow tempRow;
DataSet souceDataSet = new DataSet ();

Copy DataSet

DataSet object = souceDataSet. Copy (); // Deep Copy
DataSet object = souceDataSet. Clone (); // copying architecture only

Copy DataTable

ObjectTable = sourceTable. Copy (); // Deep Copy
ObjectTable = sourceTable. Clone (); // replication-only Architecture

Copy DataRow

This error is often encountered during project development-"This row already belongs to another table ". The statement that causes this error is as follows:

 


ObjectTable. Rows. Add (SourceDataRow );
 

The reason is analyzed because DataRow able is called by reference. Therefore, if a row is in one table, it cannot be added to another table.

 

Specific method:

 

1 ImportRow method: public void ImportRow (DataRow );

ObjectTable = sourceTable. clone (); // you must first copy the schema of the table to have the same columns or relationships!
Foreach (DataRow oRow in sourceTable)

ObjectTable. ImportRow (oRow); // add a new row to objectTable and copy the value of sourceRow. The table structure must be the same!

}

_____________________________________________________________________________________________________

2. Loop every column of the DataTable

DataRow aDataRow = objectTable. NewRow ();

Foreach (DataColumn aDataColumn in sourceTable. Columns)

    {

ADataRow [aDataColumn. ColumnName] = sourceTable [I] [aDataColumn. ColumnName];

    }

ObjectTable. Rows. Add (aDataRow );

3. Custom replication

ObjectTable. Columns. Add ("id"); // You do not need to have the same architecture. Just copy the Columns you need!
Object [] myArry = new Object [1];
Foreach (DataRow oRow in sourceTable)

TempRow = objectTable. NewRow (); // This method must be called!
MyArry [0] = oRow ["id"]; // if the id column in The Source table does not exist in myArry, an error is returned!
TempRow. ItemArray = myArry; // The ItemArray attribute is an array of the Object type. You can copy the data of multiple columns as needed!
ObjectTable. Rows. Add (tempRow); // This method must be called; otherwise, data in DataRow cannot be displayed!


_____________________________________________________________________________________________________

4. LoadDataRow method: public DataRow LoadDataRow (Object [] values, bool fAcceptChanges );

Object [] newRow = new Object [3];
// Set the object array value
NewRow [0] = "Hello ";
NewRow [1] = "World ";
NewRow [2] = "two ";
DataRow myRow;
ObjectTable. BeginLoadData ();
// Add a new row to the table
MyRow = ObjectTable. LoadDataRow (newRow, true); // the flag must be set to true to add a new row.
ObjectTable. EndLoadData ();


This method is complex. We recommend that you do not use it if you simply copy data from existing rows to add new rows. For more information about the usage, see The sdk document.

 

 

Or:

<Type = "text/JavaScript"> alimama_pid = "plain"; alimama_titlecolor = "0000FF"; expires = "000000"; alimama_bgcolor = "FFFFFF"; alimama_bordercolor = "E6E6E6 "; alimama_linkcolor = "008000"; alimama_bottomcolor = "FFFFFF"; alimama_anglesize = "0"; bandwidth = "0"; alimama_icon = "0"; bandwidth = "16"; alimama_width = 658; alimama_height = 60; alimama_type = 2; <src = "http://a.alimama.cn/inf.js" type = text/javascript>

We often need to add a row of data to the DataTable. In most cases, we add some data collected from the UI controls and program variables to the DataTable. Add the supplier code and name to the able as follows:

DataTable dtProvider = new DataTable ();
DataRow drRow = dtProvider. NewRow ();
DrRow [0] = txtProviderCode. Text. Trim ();
DrRow [1] = txtProviderName. Text. Trim ();
DtProvider. Rows. Add (drRow );

 

In most cases, these statements fully meet the requirements. However, if you want to add a row of another DataTable with the same structure to this dtProvider, you cannot simply add it. Otherwise, an error "This row belongs to another table." is displayed .". At this time, we must define another DataRow, assign the data of the source DataRow to the destination DataRow, and then Add the data to the able. As follows:

DataTable dtProvider = new DataTable ();
DataRow drTarget = dtProvider. NewRow ();
DrTarget. ItemArry = drSource. ItemArry;
// Note: The drSource is a row in another DataTable with the same structure.
DtProvider. Rows. Add (drTarget );
<Type = "text/JavaScript"> alimama_pid = "courier"; alimama_type = "f"; alimama_sizecode = "tl_1x5_8"; alimama_fontsize = 12; alimama_bordercolor = "FFFFFFFF "; alimama_bgcolor = "FFFFFF"; alimama_titlecolor = "0000FF"; alimama_underline = 0; alimama_height = 22; alimama_width = 0; <src = "http://a.alimama.cn/inf.js" type = text/javascript>

A common method for assigning values to another able

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.