遍历datatable的几种方法(C# )

转载

  1. 遍历datatable的方法2009-09-08 10:02方法一:     
  2. DataTable dt = dataSet.Tables[0];     
  3. for(int i = 0 ; i < dt.Rows.Count ; i++)     
  4. {     
  5.    string strName = dt.Rows[i]["字段名"].ToString();     
  6. }     
  7.     
     
  8. 方法二:     
  9. foreach(DataRow myRow in myDataSet.Tables["temp"].Rows)     
  10. {     
  11.       var str = myRow[0].ToString();     
  12. }     
  13.     
     
  14. 方法三:     
  15. foeach(DataRow dr in dt.Rows)        
  16. {        
  17.      object value = dr["ColumnsName"];        
  18. }     
  19.     
     
  20. 方法四:     
  21. DataTable dt=new DataTable();        
  22. foreach(DataRow dr in dt.Rows)        
  23. {      
  24.    for(int i=0;i<dt.Columns.Count;i++)      
  25.    {      
  26. dr[i];      
  27.    }        
  28. }     
  29.     
     
  30. 绑定DataTable到Reapter。     
  31. if (dtb_xx.Rows.Count > 0)     
  32.         {     
  33.             rp_xx.DataSource = dtb_xx;     
  34.             rp_xx.DataBind();     
  35.         }      
  36. <asp:Repeater ID="rp_xx" runat="server">     
  37.        <ItemTemplate>     
  38.             <tr>     
  39.                   <td>     
  40.                       <div>     
  41.                             <ul class="ListHorizontally">     
  42.                                  <li>     
  43.                                       <div class="TCell1">     
  44.                                             <%#Eval("ID")%>     
  45.                                        </div>     
  46.                                   </li>     
  47.                                   <li>     
  48.                                        <div class="TCell2">     
  49.                                              <%#Eval("Name")%>     
  50.                                         </div>     
  51.                                   </li>     
  52.                             </ul>     
  53.                        </div>     
  54.                     </td>     
  55.                 </tr>     
  56.             </ItemTemplate>     
  57.          </asp:Repeater>     
  58. 方法五  
  59. DataRow[] dataRows = null;  
  60. dataRows = dataTable.Select(fieldParentID + "='" + treeNode.Tag.ToString() + "'", dataTable.DefaultView.Sort);  
  61. foreach (DataRow dataRow in dataRows)  
  62. {   
  63.   
     
  64.      DataRow dataRow = dataTable.Rows[i];   
  65.   
     
  66.      ?? = dataRow[fieldParentID].ToString();  
  67. }   
上一篇:TCP和UDP的"保护消息边界" (经典)


下一篇:Java遍历集合的几种方法