/// <summary> /// productReturned() /// An event handler used to handle events called "eventProductChosen" on FrmCatalog /// If product is returned, it captures its properties to create an order detail object /// Order detail object then added to a datagridview control /// </summary> /// <param name="product">Object of Product></param> private void productReturned(Product product) { this.Enabled = true;//enable this form if (product == null)//return if a product is not selected return; //add order detail to grid OrderDetail orderDetail; const Int16 DEFAULT_QUANTITY = 1; foreach (DataGridViewRow currentRow in gridViewOrderDetails.Rows)//loop through gridlist { orderDetail = (OrderDetail)currentRow.Tag; //if product exists in datagridview; if (orderDetail.ProductID == product.ProductID) { orderDetail.Quantity += 1;// increase its quantity by 1 currentRow.Cells[colQuantity.Name].Value = orderDetail.Quantity.ToString();//display new value in grid cell return;//exit method } } //if product does not exist in datagridview already, create new order detail object and set its properties orderDetail = new OrderDetail(); orderDetail.ProductName = product.ProductName; orderDetail.ProductID = product.ProductID; orderDetail.Quantity = DEFAULT_QUANTITY; orderDetail.UnitPrice = product.UnitPrice; int rowNum = gridViewOrderDetails.Rows.Add(1);//adds a new row and return its index DataGridViewRow row = gridViewOrderDetails.Rows[rowNum];//sets row equal to newly created row //populate cells with order details and product name row.Cells[colProduct.Name].Value = orderDetail.ProductName; row.Cells[colProductID.Name].Value = orderDetail.ProductID; row.Cells[colUnitPrice.Name].Value = orderDetail.UnitPrice.ToString("F"); row.Cells[colQuantity.Name].Value = 1; row.Tag = orderDetail; //set first quantity item control for edit gridViewOrderDetails.CurrentCell = gridViewOrderDetails.Rows[0].Cells[colQuantity.Name]; gridViewOrderDetails.BeginEdit(true); }
/// <summary> /// InsertOrderDetails() /// Takes an OrderDetail object and inserts its properties into the Order Details Table. /// </summary> /// <param name="orderDetail"></param> public void InsertOrderDetails(OrderDetail orderDetail) { String insertQuery = "INSERT INTO [Order Details](OrderID, ProductID, UnitPrice, Quantity) VALUES (" + orderDetail.OrderID.ToString() + "," + orderDetail.ProductID.ToString() + "," + orderDetail.UnitPrice.ToString() + "," + orderDetail.Quantity.ToString() + ")"; OleDbCommand cmInsert = new OleDbCommand(insertQuery, con); try { con.Open(); cmInsert.ExecuteNonQuery(); } catch (Exception) { //can't display any error feedback in data access layer } finally { con.Close(); } }