コード例 #1
0
        private void BtnModify_Click(object sender, EventArgs e)
        {
            // get the key of the current product in the data grid view
            int rowNum  = orderDataGridView.CurrentCell.RowIndex;                             // index of the current row
            int orderID = (int)orderDataGridView["dataGridViewTextBoxColumn1", rowNum].Value; //Column for orderID

            Order currentOrder;

            using (OrdersDataContext dbContent = new OrdersDataContext())
            {
                currentOrder = (from o in dbContent.Orders
                                where o.OrderID == orderID
                                select o).Single();
            }
            FrmModifyOrder secondForm = new FrmModifyOrder
            {
                currentOrder = currentOrder
            };
            DialogResult result = secondForm.ShowDialog();

            if (result == DialogResult.OK || result == DialogResult.Retry) // successful update or concurrency exception
            {
                RefreshGridView();
            }
        }
コード例 #2
0
 private void FrmOrders_Load(object sender, EventArgs e)
 {
     using (OrdersDataContext dbContent = new OrdersDataContext())
     {
         orderDataGridView.DataSource = dbContent.Orders;
     }
 }
コード例 #3
0
        private void BtnSave_Click(object sender, EventArgs e)
        {
            try
            {
                mPreviousTime = currentOrder.ShippedDate.Value;
                using (OrdersDataContext dbContext = new OrdersDataContext())
                {
                    // get the order with OrderID from the current text box
                    Order order = dbContext.Orders.Single(o => o.OrderID == Convert.ToInt32(orderIDTextBox.Text));
                    if (order != null)
                    {
                        //make changes by copying the value from the shipped date datetimepicker box
                        order.ShippedDate = Convert.ToDateTime(shippedDateTimePicker.Text);
                        DialogResult dr = MessageBox.Show("Change the current date to " +
                                                          order.ShippedDate.Value.ToLongDateString() + " from " +
                                                          currentOrder.ShippedDate.Value.ToLongDateString() + "?",

                                                          "Confirm Date Change", MessageBoxButtons.YesNo);
                        if (dr == DialogResult.Yes)
                        {
                            if (currentOrder.OrderDate < order.ShippedDate &&
                                currentOrder.RequiredDate > order.ShippedDate &&
                                currentOrder.OrderDate != null &&
                                currentOrder.RequiredDate != null)
                            {
                                //submit changes to the database
                                dbContext.SubmitChanges();
                                order.ShippedDate = mPreviousTime;
                                DialogResult      = DialogResult.OK;
                            }
                            else
                            {
                                MessageBox.Show("The new shipped date should be between" + " "
                                                + currentOrder.OrderDate.Value.ToLongDateString() + " " +
                                                "and" + " " + currentOrder.RequiredDate.Value.ToLongDateString(), "Entry Error");
                                mPreviousTime = Convert.ToDateTime(currentOrder.ShippedDate);
                            }
                        }
                        else
                        {
                            mPreviousTime = Convert.ToDateTime(currentOrder.ShippedDate);
                        }
                    }
                }
            }
            catch (ChangeConflictException)
            {
                MessageBox.Show("Another user changed or deleted the current record", "Concurrency Exception");
                DialogResult = DialogResult.Retry;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.GetType().ToString());
            }
        }
コード例 #4
0
 private void BtnSelect_Click(object sender, EventArgs e)
 {
     using (OrdersDataContext dbContent = new OrdersDataContext())
     {
         int rowNum  = orderDataGridView.CurrentCell.RowIndex;                             //index of the current row
         int orderID = (int)orderDataGridView["dataGridViewTextBoxColumn1", rowNum].Value; //Column for orderID
         //assign order details to the orderID data grid
         orderdetailsdataGridView.DataSource =
             from orders in dbContent.Order_Details
             where orders.OrderID == orderID
             select new
         {
             orders.OrderID,
             orders.ProductID,
             orders.UnitPrice,
             orders.Quantity,
             orders.Discount,
             Total = orders.Quantity * orders.UnitPrice * (1 - Convert.ToDecimal(orders.Discount))
         };
     }
 }
コード例 #5
0
        private void RefreshGridView()
        {
            OrdersDataContext dbContext = new OrdersDataContext(); //saves the data in the database

            orderDataGridView.DataSource = dbContext.Orders;       // shows the saved data in the form
        }