关于在DataTable中执行DataTable.Select("条件")返回DataTable的解决方法
在实际编程工程中,常常遇到这样的情况:DataTable并不是数据库中的,或者DataTable尚未写到数据库,或者从数据库中读出的DataTable已经在本地被改动,又没有写回数据库(可能还要作其他改动),在这些情况下,其实只要用.NET类库中提供的DataView类的强大功能(主要是用它的RowFilter属性),就能方便地解决这类查询问题。
常有网友在网上询问怎么在DataTable中执行DataTable.Select("条件")返回DataTable,今天我在这里给个解决方法给大家参考:
代码如下
 /// <summary>
 /// <summary> /// 执行DataTable中的查询返回新的DataTable
        /// 执行DataTable中的查询返回新的DataTable /// </summary>
        /// </summary> /// <param name="dt">源数据DataTable</param>
        /// <param name="dt">源数据DataTable</param> /// <param name="condition">查询条件</param>
        /// <param name="condition">查询条件</param> /// <returns></returns>
        /// <returns></returns> private DataTable GetNewDataTable(DataTable dt,string condition)
        private DataTable GetNewDataTable(DataTable dt,string condition) {
        {             DataTable newdt = new DataTable();
            DataTable newdt = new DataTable();  newdt=dt.Clone();
            newdt=dt.Clone(); DataRow[] dr = dt.Select(condition);
            DataRow[] dr = dt.Select(condition);  for(int i=0;i<dr.Length;i++)
            for(int i=0;i<dr.Length;i++)  {
            {  newdt.ImportRow((DataRow)dr[i]);
                newdt.ImportRow((DataRow)dr[i]); }
            }  return newdt;//返回的查询结果
            return newdt;//返回的查询结果 }
        } 
                     
                    
                 
                    
                 
 
        

 
             
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号