主要思路就是实例化一个视图,对视图进行排序
/// <summary> /// DataTable排序 /// </summary> /// <param name="dt">要处理的DataTable</param> /// <param name="sortColumn">要根据什么字段排序</param> /// <param name="sortType">排序方式,1为倒序</param> /// <returns></returns> public DataTable sortDataSet(DataTable dt, string sortColumn, int sortType=0) { DataView view = null; DataTable ndt=null; string sortTypeStr = "ASC"; if(sortType==1){ sortTypeStr = "DESC"; } if (dt!=null) { view = new DataView(dt); view.Sort = "["+sortColumn+"] " + sortTypeStr; //把DataView转为DataTable,相关代码请参考https://www.yangguangdream.com/?id=2171 ndt = DataViewToDataTable(view,dt.TableName); } return ndt; } /// <summary> /// 转换DataView至DataTable /// </summary> /// <param name="dv">要处理的DataView</param> /// <param name="tableName">DataTable的名称</param> /// <returns></returns> public DataTable DataViewToDataTable(DataView dv,string tableName="Table") { //Clone dv.Table to dtTemp DataTable dtTemp = dv.Table.Clone(); dtTemp.TableName = tableName; foreach (DataRowView drv in dv) dtTemp.ImportRow(drv.Row); return dtTemp; }

微信扫码查看本文
发表评论