DataRow To DataTable

1. 方式一  導入row,新表架構與原表相同,foreach前clone, newDataTable=oldDataTable.Clone();express

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. Addspa

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

 

4. DataViewcode

// 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. Mergeblog

// 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.string

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

 

7. Linqit

if (dataRows != null && dataRows.Length > 0)
{
   dataTable = dataRows.AsEnumerable().CopyToDataTable();
}
相關文章
相關標籤/搜索