2013-11-02 39 views
7

如何循環每一行的循環?在我的代碼中,由於相同的productID,行不會綁定到下一行,所以datagridview不會移動到新行,它仍然在同一行並覆蓋價格(對於某些產品,我有兩個價格) 。如何循環顯示相同的productID,但價格不同。在datagridview中循環顯示每行

EX:1漢堡包有2個價格 漢堡包:1美元和2美元 當我得到的數據循環它,結果應該有2行相同的產品,但不同的價格。這個怎麼做?下面是我的代碼

productID = odr["product_id"].ToString(); 
quantity = Double.Parse(odr["quantity"].ToString()); 

//processing data... 
key = productID.ToString(); 

if (key != key_tmp) 
{ 
    //update index... 
    i++; 

    //updating variable(s)... 
    key_tmp = key; 
} 

if (datagridviews.Rows[i].Cells["qty"].Value != null) //already has value... 
{ 
    currJlh = Double.Parse(ddatagridviews.Rows[i].Cells["qty"].Value.ToString()); 
} 
else //not yet has value... 
{ 
    currQty = 0; 
} 
currQty += qty; 

//show data... 
datagridviews.Rows[i].Cells["price"].Value = cv.toAccountingCurrency_en(Double.Parse(odr["item_price"].ToString())); 
MessageBoxes.Show(i.ToString()); 
MessageBoxes.Show(datagridviews.Rows[i].Cells["price"].Value.ToString()); // in here there is two price that looped but won't showed in datagridviews 

幫助我:)

+0

最好展示一幅你想要的東西的照片,或者你可以更清楚地表明你真正想要的東西。我在你的文章中所理解的是,在你的DataGridView中,你想獲得相同產品ID的Total Quantity,但是你想要顯示兩個或多個價格爲一個? – Edper

回答

37

您可以通過DataGridView循環使用Rows屬性,如:

foreach (DataGridViewRow row in datagridviews.Rows) 
{ 
    currQty += row.Cells["qty"].Value; 
    //More code here 
} 
-1

最佳的形式給出了:

private void grid_receptie_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
    { 
     int X = 1; 
     foreach(DataGridViewRow row in grid_receptie.Rows) 
     { 
      row.Cells["NR_CRT"].Value = X; 
      X++; 
     } 
    } 
+0

擰後抱歉,是關於autoincrement列datagridview的其他主題 – explorercris