首页 > 其他 > 详细

DataRow To DataTable

时间:2017-12-02 13:44:29      阅读:265      评论:0      收藏:0      [点我收藏+]

1. 方式一  导入row,新表架构与原表相同,foreach前clone, newDataTable=oldDataTable.Clone();

foreach (DataRow row in rowArray) {
   dataTable.ImportRow(row);
}

2. 方式二  CopyToDataTable

DataTable dt = new DataTable();
DataRow[] dr = dt.Select("Your string");
DataTable dt1 = dr.CopyToDataTable();

if (dr.Length > 0)
    DataTable dt1 = dr.CopyToDataTable();
//https://msdn.microsoft.com/en-us/library/bb396189.aspx

3. Add

DataTable dt = new DataTable(); 
DataRow[] dr = (DataTable)dsData.Tables[0].Select("Some Criteria");
dt.Rows.Add(dr);

 

4. DataView

// Create a DataTable
DataTable table = new DataTable()

// Filter and Sort expressions
string expression = "[Birth Year] >= 1983"; 
string sortOrder = "[Birth Year] ASC";

// Create a DataView using the table as its source and the filter and sort expressions
DataView dv = new DataView(table, expression, sortOrder, DataViewRowState.CurrentRows);

// Convert the DataView to a DataTable
DataTable new_table = dv.ToTable("NewTableName");

5. Merge

// dtData is DataTable that contain data
DataTable dt = dtData.Select("Condition=1").CopyToDataTable();

// or existing typed DataTable dt
dt.Merge(dtData.Select("Condition=1").CopyToDataTable());

 

6.

DataTable dt = myDataRowCollection.CopyToDataTable<DataRow>();

 

7. Linq

if (dataRows != null && dataRows.Length > 0)
{
   dataTable = dataRows.AsEnumerable().CopyToDataTable();
}

 

DataRow To DataTable

原文:http://www.cnblogs.com/dennysong/p/7953858.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!