23

DataGridView読み込んだa の各行をループするにはどうすればよいですか? 私のコードでは、productID が同じであるため、行は次の行にバインドされDataGridViewないため、新しい行に移動しません。同じ行にとどまり、価格を上書きします (一部の製品では、2 つの価格があります)。各行をループして、同じ productID を表示するが価格が異なるようにするにはどうすればよいですか?

例: 1 つのハンバーガーには、1 ドルと 2 ドルの 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
4

3 に答える 3

78

次のように、プロパティをDataGridView使用してループできます。Rows

foreach (DataGridViewRow row in datagridviews.Rows)
{
   currQty += row.Cells["qty"].Value;
   //More code here
}
于 2013-11-02T00:21:06.890 に答える