📄 orders.cs
字号:
}
/// Create Order data view if it does not exist.
/// Set the row filter based on the customer ID that the user selected on the Customers control.
///
if (null == this.dvOrders)
{
this.dvOrders = new DataView(dtOrders);
this.dvOrders.RowFilter = String.Format("CustomerID = '{0}'", this.customerID);
}
/// Starts the cursor icon because this function might take some time.
///
Cursor.ShowWaitCursor(true);
try
{
try
{
/// Load order details data based on the order ID.
///
this.dtOrderDetails = this.dataIBuySpy.LoadOrderDetails(this.orderID);
}
finally
{
/// Set cursor back to normal.
///
Cursor.ShowWaitCursor(false);
}
}
catch(SqlCeException e)
{
IBuySpyData.ShowErrors(e);
return;
}
catch(Exception e)
{
MessageBox.Show("Init order details: " + e.Message, "IBuySpy Delivery");
return;
}
/// Display customer name.
///
this.lblCustomerValue.Text = this.customerName;
/// Binds the Order ID values to the controls
///
this.cboOrderIDs.DisplayMember = "OrderID"; /// Define the field to be displayed.
this.cboOrderIDs.ValueMember = "OrderID"; /// Define the field to be used as the value.
this.cboOrderIDs.DataSource = this.dvOrders; /// Populate the list.
/// Select the order ID.
///
this.cboOrderIDs.SelectedValue = this.orderID;
/// Clear the data binding.
///
this.lblOrderDateValue.DataBindings.Clear();
this.lblQuantityValue.DataBindings.Clear();
this.lblPriceValue.DataBindings.Clear();
/// Bind the datatable values to the property "Text" of the controls.
///
this.lblOrderDateValue.DataBindings.Add("Text", this.dvOrders, "OrderDate");
this.lblQuantityValue.DataBindings.Add("Text", dtOrderDetails, "Quantity");
this.lblPriceValue.DataBindings.Add("Text", dtOrderDetails, "UnitCost");
/// Bind the Product values to the controls.
///
this.lstProducts.DisplayMember = "ModelName";
this.lstProducts.ValueMember = "ProductID";
this.lstProducts.DataSource = this.dtOrderDetails;
/// Update button controls based on order status.
///
UpdateControls();
/// Update the total cost value.
///
UpdateTotal();
/// Reset the initialization flag.
///
init = false;
}
/// Reset the Orders data view row filter based on the new customer ID.
/// Update the customer name.
/// Load order details data.
///
internal void RefreshOrders()
{
this.dvOrders.RowFilter = String.Format("CustomerID = '{0}'", this.customerID);
this.lblCustomerValue.Text = this.customerName;
this.cboOrderIDs.SelectedValue = this.orderID;
RefreshOrderDetails();
}
/// Load order details data.
///
private void RefreshOrderDetails()
{
/// Clear the text of the controls.
///
this.lblQuantityValue.Text = String.Empty;
this.lblPriceValue.Text = String.Empty;
this.lblSubTotalValue.Text = String.Empty;
try
{
/// Load order details.
///
this.dataIBuySpy.LoadOrderDetails(this.orderID);
}
catch(SqlCeException e)
{
IBuySpyData.ShowErrors(e);
}
catch(Exception e)
{
MessageBox.Show("Load order details: " + e.Message, "IBuySpy Delivery");
}
/// Update button controls based on the order status.
///
UpdateControls();
/// Update the total cost value.
///
UpdateTotal();
}
/// Updates the buttons on the Orders control. The Add More button is enabled only if the order status is pending.
/// The Signature button is disabled only if the Order status is "Failed".
///
internal void UpdateControls()
{
DataRowView row = null;
OrderStatus orderStatus = OrderStatus.Failed;
if (0 == this.dvOrders.Count)
{
return;
}
/// Set the current binding position.
///
BindingContext[this.dvOrders].Position = this.cboOrderIDs.SelectedIndex;
/// Get the current row.
///
row = (DataRowView)BindingContext[this.dvOrders].Current;
/// Get status of the selected order.
///
orderStatus = (OrderStatus)Convert.ToInt32(row["Status"]);
/// If it is a pending order, enable the AddMore and Signature buttons.
///
if (OrderStatus.Pending == orderStatus)
{
this.btnAddMore.Enabled = true;
this.btnSignature.Enabled = true;
}
/// If the order is delivered, disable the AddMore button and enable the Signature button.
///
else if (OrderStatus.Delivered == orderStatus)
{
this.btnAddMore.Enabled = false;
this.btnSignature.Enabled = true;
}
/// Otherwise, make both the AddMore and Signature buttons unavailable.
///
else
{
this.btnAddMore.Enabled = false;
this.btnSignature.Enabled = false;
}
}
/// Update the total cost for the selected order.
///
internal void UpdateTotal()
{
try
{
this.lblTotalValue.Text = this.dataIBuySpy.OrderTotal().ToString("C");
}
catch(SqlCeException e)
{
IBuySpyData.ShowErrors(e);
}
catch(Exception e)
{
MessageBox.Show("Update total: " + e.Message, "IBuySpy Delivery");
}
}
/// Update the subtotal cost for the selected product.
///
internal void UpdateSubTotal()
{
int quantity = Convert.ToInt32(this.lblQuantityValue.Text);
decimal unitCost = Convert.ToDecimal(this.lblPriceValue.Text);
decimal subTotal = quantity*unitCost;
this.lblSubTotalValue.Text = subTotal.ToString("C");
}
/// Users can select different orders using this combo box (users can also select different orders from the
/// Customers control). When a different order is selected, the new information for that order must
/// be displayed.
///
private void cboOrderIDs_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (!init &&
0 <= this.cboOrderIDs.SelectedIndex &&
this.orderID.ToString() != this.cboOrderIDs.Text)
{
/// If the order has been modified, switching orders will discard all changes; therefore, ask the user to confirm.
///
if (this.dataIBuySpy.HasChanges())
{
if (DialogResult.OK == MessageBox.Show(String.Format("You have modified order {0}. Switching orders will discard all changes.", this.orderID),
"IBuySpy Delivery",
MessageBoxButtons.OKCancel,
MessageBoxIcon.Asterisk,
MessageBoxDefaultButton.Button1))
{
/// Discard the changes and reset the order details to its original value.
///
this.dataIBuySpy.ResetOrderDetails();
}
else
{
/// Cancel the new order selection.
///
this.cboOrderIDs.SelectedValue = this.orderID;
return;
}
}
/// Set the selected Order ID.
///
this.orderID = Convert.ToInt32(this.cboOrderIDs.Text);
/// Refresh order details info.
///
RefreshOrderDetails();
}
}
/// Update the subtotal if a different product is selected. Only the subtotal must be manually
/// updated; the price and quantity are automatically updated because they are bound to the OrderDetails datatable.
///
private void lstProducts_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (0 <= this.lstProducts.SelectedIndex)
{
/// Set the current binding position.
///
BindingContext[this.dtOrderDetails].Position = this.lstProducts.SelectedIndex;
/// Update the subtotal cost for the selected product.
///
UpdateSubTotal();
}
}
/// Display the Inventory control with the current order associated.
///
private void btnAddMore_Click(object sender, System.EventArgs e)
{
InventoryEventArgs args = new InventoryEventArgs(this.orderID);
OnViewInventory(this, args);
}
/// Display the Signature control with the current OrderID and status associated.
///
private void btnSignature_Click(object sender, System.EventArgs e)
{
if (0 == this.dvOrders.Count)
{
return;
}
/// Get the current row.
///
DataRowView row = (DataRowView)BindingContext[this.dvOrders].Current;
SignatureEventArgs args = new SignatureEventArgs(this.orderID, (OrderStatus)Convert.ToInt32(row["Status"]));
OnViewSignature(this, args);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -