2012-08-02 87 views
1

使用CSharp,我想訪問數據表的每個單元格。我怎樣才能做到這一點?我想訪問數據表的每個單元格。怎麼樣?

for (int i = 0; i <= 5; i++) 
    { 
     if (myQuantity[i]!=null && myQuantity[i].Length>0) 
     { 
      row = dt.NewRow(); 
      row["Name"] = myName[i]; 
      row["Quantity"] = myQuantity[i]; 
      row["Price"] = myPrice[i]; 

      c = Convert.ToInt32(myQuantity[i]); 

      int price = Convert.ToInt32(myPrice[i]) * c; 


      row["Amount"] = price; 

      dt.Rows.Add(row); 

     } 
    } 
+0

你能更具體地瞭解你遇到的問題嗎? – 2012-08-02 10:04:29

+0

可以請詳細解釋一下嗎? – 2012-08-02 10:04:55

回答

3

您可以通過訪問每個單元的行中循環:

int numberOfColumns = dt.Columns.Count; 

// go through each row 
foreach (DataRow dr in dt.Rows) 
{ 
    // go through each column in the row 
    for (int i = 0; i < numberOfColumns; i++) 
    { 
     // access cell as set or get 
     // dr[i] = "something"; 
     // string something = Convert.ToString(dr[i]); 
    } 
} 
+0

它的工作原理。我可以訪問每一行的每個單元格。非常感謝你 :) – Fahad 2012-08-02 10:24:35

5

可以使用foreach環路DataRow以及DataColumn,以下行的東西...

DataTable dt = new DataTable(); 

foreach(DataRow dr in dt.Rows) 
    foreach (DataColumn dc in dt.Columns) 
    { 
     Console.WriteLine(dr[dc]); 
    } 
相關問題