private void button_modify_Click(object sender, EventArgs e) //修改订单 { Form_modifyOrder modifyOrderForm = new Form_modifyOrder(); DialogResult dialogResult = modifyOrderForm.ShowDialog(); if (dialogResult == DialogResult.OK) { for (int i = 0; i < dataGridView1.Rows.Count; i++) { if (dataGridView1.Rows[i].Cells[4].Value == null) { dataGridView1.Rows[i].Cells[4].Value = false; continue; } if ((bool)dataGridView1.Rows[i].Cells[4].Value == true) { bindingSource1.List.RemoveAt(i); bindingSource1.List.Insert(i, modifyOrderForm.modifyOrder); } } button_delete.Enabled = false; button_delete.Visible = false; button_modify.Enabled = false; button_modify.Visible = false; dataGridView1.Columns[4].Visible = false; } }
private void button_modify_Click(object sender, EventArgs e) //修改订单 { Form_modifyOrder modifyOrderForm = new Form_modifyOrder(); DialogResult dialogResult = modifyOrderForm.ShowDialog(); if (dialogResult == DialogResult.OK) { for (int i = 0; i < dataGridView1.Rows.Count; i++) { if (dataGridView1.Rows[i].Cells[4].Value == null) { dataGridView1.Rows[i].Cells[4].Value = false; continue; } if ((bool)dataGridView1.Rows[i].Cells[4].Value == true) { int num = (int)dataGridView1.Rows[i].Cells[0].Value; using (var db = new OrderModel()) { var order = db.Orders.Include("OrderDetails").First(p => p.orderNumber == num); //在数据库中找到一个订单num对应的订单 if (order != null) { order.guest = modifyOrderForm.modifyOrder.guest; order.orderAddress = modifyOrderForm.modifyOrder.orderAddress; var orderdetails = db.OrderDetails.Where(p => p.Order.orderNumber == order.orderNumber); foreach (OrderDetails orderDetails1 in orderdetails) { db.OrderDetails.Remove(orderDetails1); } db.SaveChanges(); //order.orderDetails.Clear(); int cou = 1; foreach (var good in modifyOrderForm.goods) //遍历字典,将所有商品及数量添加到订单中 { OrderDetails orderDetails = new OrderDetails(good.Key, good.Value); orderDetails.OrderId = order.orderNumber; orderDetails.Order = order; orderDetails.OrderDetailsId = int.Parse(order.orderTime.Day + "" + cou++); order.orderDetails.Add(orderDetails); } order.reCalculatePrice(); db.SaveChanges(); } } //bindingSource1.List.Insert(i, modifyOrderForm.modifyOrder); } } using (var db = new OrderModel()) { bindingSource1.DataSource = db.Orders.ToList <Order>(); } button_delete.Enabled = false; button_delete.Visible = false; button_modify.Enabled = false; button_modify.Visible = false; dataGridView1.Columns[4].Visible = false; } }