private void Form1_Load(object sender, EventArgs e) { List <Order> ord = new List <Order>(); ord = OrderDB.GetOrders(); orderBindingSource.DataSource = ord; }
// method for getting data for the orders and displaying orders private void DisplayOrders() { List <Order> ord = new List <Order>(); try { ord = OrderDB.GetOrders(); orderBindingSource.DataSource = ord; } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } }
private void btnSaveDate_Click(object sender, EventArgs e) { try { OrderDB.SaveShippedDate(orders[recordIndex], tmpDate); // call the Save method orders = OrderDB.GetOrders(); // reload the orders list LoadOrders(recordIndex); // redisplay the information based on the current record btnSaveDate.Enabled = false; // disable the button as we are done this operation } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
// method for getting data for an order private Order GetOrder(int orderId) { Order ord = null; try { ord = new Order(); ord = OrderDB.GetOrder(orderId); } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } return(ord); }
/// <summary> /// Method to refresh the list in any situation where it's needed /// </summary> private void RefreshList() { try { orders = OrderDB.GetOrders(); // get an updated list of orders from the database dgvOrders.DataSource = orders; // put it in the data grid view dgvOrders.Columns[2].DefaultCellStyle.Format = "d"; // set the date columns to short date format dgvOrders.Columns[3].DefaultCellStyle.Format = "d"; dgvOrders.Columns[4].DefaultCellStyle.Format = "d"; } catch (Exception ex) { MessageBox.Show("Error loading the order list: " + ex.Message, ex.GetType().ToString()); } }
// if the user confirm the changes private void btnConfirm_Click(object sender, EventArgs e) { // check if the new shipped date is valid (in range) or it is null if (IsValidData(DTPShippedDate) || (DTPShippedDate.Value == null)) { // creating a new order and setting its properties Order newOrder = new Order(); GetNewOrder(newOrder); // if the user wants to delete the shipped date if (delete == true) { newOrder.ShippedDate = null; } else { // the new order has a different shipped date newOrder.ShippedDate = Convert.ToDateTime(DTPShippedDate.Value); } // try to update data in the database try { if (!OrderDB.UpdateOrder(newOrder, order)) { MessageBox.Show("Another user has updated or " + "deleted that customer.", "Database Error"); this.DialogResult = DialogResult.Retry; } else // successfully updated { order.ShippedDate = newOrder.ShippedDate; this.DialogResult = DialogResult.OK; } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } }
/// <summary> /// Save changes to the shipping date for this order /// </summary> private void btnSave_Click(object sender, EventArgs e) { // first, have to check if the date is null or invalid (treat as null) before trying to parse it DateTime newDate; // somewhere to put the result bool parseResult = DateTime.TryParse(txtShippedDate.Text, out newDate); if (parseResult) // if the parse was successful (it's a legitimate date), compare against the other dates to make sure it's between them { if ((CurrentOrder.OrderDate != null && CurrentOrder.OrderDate > newDate) || (CurrentOrder.RequiredDate != null && CurrentOrder.RequiredDate < newDate)) { MessageBox.Show("Shipping Date must be later than order date and earlier than Required date"); // notify the user txtShippedDate.Text = ""; // clear out the bad date and focus the field for correction txtShippedDate.Focus(); return; // break out of this method entirely so they can fix it } } try { if (parseResult) // if the parse worked (and it was in the correct range), send the new date { OrderDB.UpdateOrder(CurrentOrder, newDate); } else // if it's blank or isn't a date { OrderDB.UpdateOrder(CurrentOrder, null); } // if we're still here and nothing messed up, close this and go back to the order list DialogResult = DialogResult.OK; this.Close(); } catch (Exception ex) { MessageBox.Show("Error saving shipping date: " + ex.Message, ex.GetType().ToString()); } }
private void Form1_Shown(object sender, EventArgs e) { orders = OrderDB.GetOrders(); // populate the list with all the orders from the DB LoadOrders(recordIndex); // set Form controls with the first order entity information }