现在有一组dataRow要转换成DataTable,应该是用什么方法好?
新的DataTable的列必须和原来DataRow的列的属性相同!

解决方案 »

  1.   

    DataTable dt = new DataTable();
    dt.Rows.Add(/*行*/);用ADD()!
      

  2.   

    楼主是对DataTable进行Select后得到的DataRow数组吗?一般是循环,参考如下方法
    private DataTable GetNewDataTable(DataTable dt, string condition) 

        DataTable ndt = new DataTable(); 
        ndt = dt.Clone(); 
        DataRow[] dr = dt.Select(condition); 
        for (int i = 0; i <= dr.Length - 1; i++) 
      { 
            ndt.ImportRow((DataRow)dr(i)); 
        } 
        return ndt; 
    }
      

  3.   

    哦,抱歉,这是以前写的VB.NET的代码,转成C#时忘了把()改回[]了,这样private DataTable GetNewDataTable(DataTable dt, string condition) 

        DataTable ndt = new DataTable(); 
        ndt = dt.Clone(); 
        DataRow[] dr = dt.Select(condition); 
        for (int i = 0; i <= dr.Length - 1; i++) 
      { 
            ndt.ImportRow((DataRow)dr[i]); 
        } 
        return ndt; 
    }
      

  4.   

    TO:lxcnn(过客) 是对DataTable进行Select后得到的DataRow数组 ndt.ImportRow((DataRow)dr(i)); 
     是不是应该是
     ndt.ImportRow((DataRow)dr[i]); 但好像也不行!
      

  5.   

    过客和我抢分,可能我的**什么时候才能拥有啊!!
    ---------------
    哈,一个人那叫捡分,人多了才叫抢分,捡分多没意思啊,大家都来抢分,集思广益,互相学习才有进步嘛PS:我也在抢分攒下一颗星星,看看谁速度吧^o^
     ndt.ImportRow((DataRow)dr(i)); 
     是不是应该是
     ndt.ImportRow((DataRow)dr[i]); 
    ------------
    嗯,这个错误我在后面已经更正了
    但好像也不行!
    ---------
    你是怎么调用的,我的例子,测试没有问题的SqlConnection con = new SqlConnection("Data Source=CNN;Initial Catalog=test01;User ID=sa;Password=;");
    SqlDataAdapter da = new SqlDataAdapter("select id, title from test01", con);
    DataSet ds = new DataSet();
    da.Fill(ds, "mytest");
    dt = ds.Tables["mytest"];DataTable newdt = GetNewDataTable(dt, "id>2");
    dataGridView1.DataSource = newdt;
      

  6.   

    TO:lxcnn(过客) 搞定,谢谢!
      

  7.   

    DataTable mydt = DataRow.Table;