public void NewOrderPage() { byte specialDiscount = 0; tOrder o = new tOrder { OrderDate = DateTime.Now, SpecialDiscount = specialDiscount /*, CustomerID = */ }; //var q = from o in DBContext.Orders // from od in DBContext.OrderDetail // from op in DBContext.OrderPlatform }
private void save_Click(object sender, EventArgs e) { DateTime dateTime = orderDateDateTimePicker.Value; if (customerIDTextBox.Text != "" && cbxPid.Text != "" && bidFeeTextBox.Text != "" && shippingFeeTextBox.Text != "" && totalPriceTextBox.Text != "") { //以下Order分頁上半部份SAVE===================================================== tCustomer c = new tCustomer { Account = customerIDTextBox.Text, PlatformID = cbxPid.SelectedIndex + 1 }; this.DBContext.tCustomers.Add(c); this.DBContext.SaveChanges(); var cust = from cu in DBContext.tCustomers where cu.Account == customerIDTextBox.Text select cu.CustomerID; List <int> a = cust.ToList(); int oorderID = int.Parse(orderIDTextBox.Text); int total = 0; for (int i = 0; i < dataTable1DataGridView.Rows.Count - 1; i++) { string x = dataTable1DataGridView.Rows[i].Cells["SubTotal"].Value.ToString(); int z = int.Parse(x); total = z + total; } tOrder o = new tOrder { OrderDate = dateTime, PlatformID = cbxPid.SelectedIndex + 1, BidFee = int.Parse(bidFeeTextBox.Text), CustomerID = a[0], ShippingFee = int.Parse(shippingFeeTextBox.Text), TotalPrice = total }; this.DBContext.tOrders.Add(o); int save = this.DBContext.SaveChanges(); MessageBox.Show($"有 {save} 筆記錄成功新增"); //以下SAVE datagridview========================================================= //string productName; 測試中 未完成 Foregin key 問題 try { if (save > 0) { for (int i = 0; i < dataTable1DataGridView.Rows.Count - 1; i++) { int orderID = oorderID; string ProductName = dataTable1DataGridView.Rows[i].Cells["ProductName"].Value.ToString(); string ProductColor = dataTable1DataGridView.Rows[i].Cells["proudctColor"].Value.ToString(); string price = dataTable1DataGridView.Rows[i].Cells["Price"].Value.ToString(); int intPrice = int.Parse(price); string discount = dataTable1DataGridView.Rows[i].Cells["Discount"].Value.ToString(); int intDiscount = Convert.ToInt32(discount); string quantity = dataTable1DataGridView.Rows[i].Cells["Quantity"].Value.ToString(); int intQuantity = int.Parse(quantity); string sub = dataTable1DataGridView.Rows[i].Cells["SubTotal"].Value.ToString(); int intSub = int.Parse(sub); var pid = from p1 in DBContext.tNewProducts where p1.ProductName == ProductName && p1.Color == ProductColor select p1.ProductID; List <int> x = pid.ToList(); var test = new tOrderDetail { OrderID = orderID, ProductID = x[0], Price = intPrice, Discount = intDiscount, Quantity = intQuantity, subtotal = intSub }; DBContext.tOrderDetails.Add(test); this.DBContext.SaveChanges(); } } } catch { } //=========================================================== this.Read_RefreshDataGridView(); DataRefresh(); } else { MessageBox.Show("請輸入所需資料!"); } }