DataTable操作

一 复制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");//查询的结果

            if (tableAd != null && tableAd.Rows.Count > 0)

            {

                Tableflag = tableAd.Copy();//复制结构

                Tableflag.Clear();//清除临时数据

                foreach (DataRow dr in tableAd.Rows)

                {

                    if (dr["ParentId"].ToString().Equals("17"))

                    {

                        DataRow tempRow =TableTemp.NewRow(); //创建与该表相同架构的新行
              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:

DataTable操作
#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);

                    //复制DataTable dt结构不包含数据  

                    dt1 = dt.Clone();

                    foreach (DataRow row in GetRows)

                    {

                        dt1.Rows.Add(row.ItemArray);

                    }

                    return dt1;

                default:

                    return dt;

            }

        }

        #endregion  
View Code
//选取ParentId=17的所以行,并根据Sorts降序排序
TableTemp = SortDataTable(tableAd, "ParentId=17", "Sorts Desc", 2);

Three:

DataTable操作
        #region 获取DataTable前几条数据

        /// <summary>  

        /// 获取DataTable前几条数据  

        /// </summary>  

        /// <param name="TopItem">前N条数据</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行数据
TableTemp = DtSelectTop(7, Table1);

 

你可能感兴趣的:(Datatable)