private void addToCartBtn_Click(object sender, EventArgs e) { if (searchResultGrid.SelectedRows.Count < 1) { MessageBox.Show("You need to select atleast one item.", "Error while adding to cart", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } int rowsCount = searchResultGrid.SelectedRows.Count; for (int i = 0; i < rowsCount; i++) { int productId = GetProductIdByProdcutName(searchResultGrid.SelectedRows[i].Cells[0].Value.ToString()); eShopServiceReference.ProductData tmp = GetProductFromSearchResultList(productId); cartList.Add(tmp); int quantity = int.Parse(quantityComboBox.SelectedItem.ToString()); float totalCostForItem = quantity * tmp.price; itemsTotalCostList.Add(totalCostForItem); quantitiesList.Add(quantity); cartDataGrid.Rows.Add(tmp.productName, quantityComboBox.SelectedItem, totalCostForItem + " = " + tmp.price + " X " + quantity); totalPriceLbl.Text = (float.Parse(totalPriceLbl.Text) + totalCostForItem).ToString(); } }
private eShopServiceReference.ProductData GetProductFromSearchResultList(int productId) { eShopServiceReference.ProductData product = new eShopServiceReference.ProductData(); for (int i = 0; i < searchResult.Length; i++) { if (searchResult[i].productId == productId) { product.productId = searchResult[i].productId; product.productName = searchResult[i].productName; product.price = searchResult[i].price; product.keys = searchResult[i].keys; product.businessId = searchResult[i].businessId; product.businessName = searchResult[i].businessName; return(product); } } return(null); }