DataRow To DataTable

Posted 楊柳

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DataRow To DataTable相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

Linq&Lumda---LINQ to DataSet的DataTable操作

DataTable查询出DataRow数据

20191017_datatable.select() 数据源中没有dataRow

如何给DataTable,DataRow 手动赋值

.NET里面datatable.rows[i]可以直接转换为DataRow吗?是否要先new一个DataRow,然后再依次赋值?

c# datarow[] 转换成 datatable