DataTable操作
生活随笔
收集整理的這篇文章主要介紹了
DataTable操作
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
DataTable操作
tempRow["ClassName"] = dr["ClassName"];
tempRow["ParentId"] = dr["ParentId"];
tempRow["Name"] = dr["Name"];
tempRow["Pic"] = dr["Pic"];
tempRow["Url"] = dr["Url"];
tempRow["Sorts"] = dr["Sorts"];
TableTemp.Rows.Add(tempRow);
}
}
}
TableTemp = SortDataTable(tableAd, "ParentId=17", "Sorts Desc", 2);
TableTemp = DtSelectTop(7, Table1);
一 復(fù)制DataTable中符合條件的DataRow到新的DataTable中
?
One:
DataTable TableTemp = new DataTable();//臨時table DataTable tableAd = new Web.DAL.FreeBase().TranSQLGetTable("select a.ClassName,b.ParentId,b.Name,b.Pic,b.Url,b.Sorts from AdClass a inner join Ad b on a.Id=b.ParentId");//查詢的結(jié)果if (tableAd != null && tableAd.Rows.Count > 0){Tableflag = tableAd.Copy();//復(fù)制結(jié)構(gòu)Tableflag.Clear();//清除臨時數(shù)據(jù)foreach (DataRow dr in tableAd.Rows){if (dr["ParentId"].ToString().Equals("17")){DataRow tempRow =TableTemp.NewRow(); //創(chuàng)建與該表相同架構(gòu)的新行tempRow["ClassName"] = dr["ClassName"];
tempRow["ParentId"] = dr["ParentId"];
tempRow["Name"] = dr["Name"];
tempRow["Pic"] = dr["Pic"];
tempRow["Url"] = dr["Url"];
tempRow["Sorts"] = dr["Sorts"];
TableTemp.Rows.Add(tempRow);
}
}
}
Two:
#region DataTable篩選,排序返回符合條件行組成的新DataTable或直接用DefaultView按條件返回/// <summary> /// DataTable篩選,排序返回符合條件行組成的新DataTable或直接用DefaultView按條件返回 /// eg:SortExprDataTable(dt,"Sex='男'","Time Desc",1) /// </summary> /// <param name="dt">傳入的DataTable</param> /// <param name="strExpr">篩選條件</param> /// <param name="strSort">排序條件</param> /// <param name="mode">1,直接用DefaultView按條件返回,效率較高;2,DataTable篩選,排序返回符合條件行組成的新DataTable</param> public static DataTable SortDataTable(DataTable dt, string strExpr, string strSort, int mode){switch (mode){case 1://方法一 直接用DefaultView按條件返回 dt.DefaultView.RowFilter = strExpr;dt.DefaultView.Sort = strSort;return dt;case 2://方法二 DataTable篩選,排序返回符合條件行組成的新DataTable DataTable dt1 = new DataTable();DataRow[] GetRows = dt.Select(strExpr, strSort);//復(fù)制DataTable dt結(jié)構(gòu)不包含數(shù)據(jù) dt1 = dt.Clone();foreach (DataRow row in GetRows){dt1.Rows.Add(row.ItemArray);}return dt1;default:return dt;}}#endregion View Code //選取ParentId=17的所以行,并根據(jù)Sorts降序排序TableTemp = SortDataTable(tableAd, "ParentId=17", "Sorts Desc", 2);
Three:
#region 獲取DataTable前幾條數(shù)據(jù)/// <summary> /// 獲取DataTable前幾條數(shù)據(jù) /// </summary> /// <param name="TopItem">前N條數(shù)據(jù)</param> /// <param name="oDT">源DataTable</param> /// <returns></returns> public static DataTable DtSelectTop(int TopItem, DataTable oDT){if (oDT.Rows.Count < TopItem) return oDT;DataTable NewTable = oDT.Clone();DataRow[] rows = oDT.Select("1=1");for (int i = 0; i < TopItem; i++){NewTable.ImportRow((DataRow)rows[i]);}return NewTable;}#endregion View Code?
//選取前7行數(shù)據(jù)TableTemp = DtSelectTop(7, Table1);
?
posted on 2015-07-03 14:43?無影飛絮劍 閱讀(...) 評論(...) 編輯 收藏轉(zhuǎn)載于:https://www.cnblogs.com/itslives-com/p/4618627.html
總結(jié)
以上是生活随笔為你收集整理的DataTable操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html5 斗鱼 苹果,斗鱼ios端手游
- 下一篇: 计算机处理问题的数学模型分为哪两类,数据