首先 你在遍历newcity.Rows的时候再修改它 往里面加数据
我估计会跑错误的哦 foreach直接就不干活了
其次 如果你要添加数据的话就是这样
DataRow dr = newcity.NewRow();
dr["id"] = 你要的付给列id的值
newcity.Rows.Add(dr);
大概就是这样的 楼主可以试试 没有测试不保证代码一定好使
但是八九不离十吧应该

解决方案 »

  1.   

    使用循环遍历df数组,然后加入到newcity中如:
    foreach(DataRow dr in df){
      newcity.Rows.Add(dr.ItemArray);
    }
                      
      

  2.   


     DataTable newcity = new DataTable();
    DataTable newcity_bak = newcity.Clon;
                            foreach (DataRow dr in newcity.Rows)
                            {
                            
                              foreach (EN_ec_city en in enlist)
                               {
                              DataRow[] df = dtlogistice.Select(String.Format("startaddr='{0}'", en.city_name));
                              if (df.Length > 0)
                              {
     foreach (DataRow row in df){
    newcity_bak.importrows(row);
    }

      

  3.   


    不行,到newcity.Rows.Add(dr.ItemArray);就显示dr不存在了
      

  4.   

      
    这个不会把,怎么会不存在呢
     DataTable newcity = new DataTable();
                            foreach (DataRow dr in newcity.Rows)
                            {
                            
                              foreach (EN_ec_city en in enlist)
                               {
                              DataRow[] df = dtlogistice.Select(String.Format("startaddr='{0}'", en.city_name));
                              if (df.Length > 0)
                              {
    foreach(DataRow d in df){
      newcity.Rows.Add(d.ItemArray);
    }
    }
      

  5.   


    newcity.Rows这里啥都没有,怎么来个遍历
      

  6.   

    我的神啊  
     DataTable newcity = new DataTable();
    foreach (DataRow dr in newcity.Rows)
    {。}
    刚刚创建表newcity就来遍历,肯定不行啊  
      

  7.   

    首先得明确新表newcity里面有多少列,什么类型,再添加 newcity.Columns.Add("...",typeof(类型)); 
    再添加数据 new.Rows.Add(new object[]{.........................});
      

  8.   

    newgvprice.Rows.Add(row);到这句还是无法执行
    代码:    foreach (EN_ec_city en in enlist)
                                      {
                                          DataRow[] df = dtlogistice.Select(String.Format("startaddr='{0}'", en.city_name));
                                          
                                          if (df.Length > 0)
                                          {
                                              foreach (DataRow row in df)
                                              {
                                                  newgvprice.Rows.Add(row);
                                                                                            }
                                              if (newgvprice != null)
                                              {
                                                  dgvprice.DataSource = newgvprice;
                                                  MessageBox.Show("excel 导入成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                              }
                                             
                                          }
                                          
                                      }
                                      
      

  9.   

    可是还是报错,EN_ec_city是什么