DataTable Select RowFilter 資料複製到另一個DATATABLE
//Create DataTable
DataTable dt = new DataTable();
DataColumn column;
DataRow row;
// Create column.
column = new DataColumn();
column.DataType = Type.GetType("System.Int16");
column.ColumnName = "ID";
dt.Columns.Add(column);
// Create new DataRow objects and add to DataTable.
for (int i = 1; i <= 10; i++)
{
row = dt.NewRow();
row["ID"] = i.ToString();
dt.Rows.Add(row);
}
//RowFilter exsample1
dt.DefaultView.RowFilter = "ID=1";
//RowFilter exsample2
dt.DefaultView.RowFilter = "ID>1";
//sort
dt.DefaultView.Sort = "id desc";
//RowFilter Copy New Table
DataTable dtNew = dt.Clone();
foreach(DataRow dr in dt.Select("ID > 5","ID"))
{
dtNew.ImportRow(dr);
}
//Copy all row to new Table
DataTable dtNew = dt.Clone();
foreach (DataRow dr in dt.Rows)
{
dtNew.ImportRow(dr);
}
沒有留言:
張貼留言