2013-11-02 70 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