1. 程式人生 > >c# DataTable select 過濾返回新DataTable

c# DataTable select 過濾返回新DataTable

        DataRow[] dr = dt.Select(“col='XXXX'”);//條件:就是欄位名='某某'

        /// 執行DataTable中的查詢返回新的DataTable

        /// </summary>
        /// <param name="dt">源資料DataTable</param>
        /// <param name="condition">查詢條件</param>
        /// <returns></returns>
        private DataTable GetNewDataTable(DataTable dt, string condition,string sortstr)

        {
            DataTable newdt = new DataTable();
            newdt = dt.Clone();
            DataRow[] dr = dt.Select(condition,sortstr);
            for (int i = 0; i < dr.Length; i++)
            {
                newdt.ImportRow((DataRow)dr[i]);
            }
            return newdt;//返回的查詢結果
        }