Ke Vin Ke Vin -4 years ago 146
C# Question

Looping each row in datagridview

How to loop each row that readed? in my code, the row won't bind to next row because of the same productID, so the datagridview won't move to new row, it still in the same row and overwrite the price (for some product, i have two price). How to loop it to show the same productID but it have different price.

EX : 1 Hamburger have 2 price
Hamburger : $1 and $2
when i get the data with looping it, the resould shoult be have 2 Row with same product but different price. How to do this? below is my code

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

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

if (key != key_tmp)
//update index...

//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(datagridviews.Rows[i].Cells["price"].Value.ToString()); // in here there is two price that looped but won't showed in datagridviews

help me out :)

Answer Source

You could loop through DataGridView using Rows property, like:

foreach (DataGridViewRow row in datagridviews.Rows)
   currQty += row.Cells["qty"].Value;
   //More code here
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download