AdventureWorks 数据绑定示例添加 SalesOrderDetail 窗体(EDM 示例应用程序)
AdventureWorks 数据绑定应用程序使用单独的 Windows 窗体添加一个新的 SalesOrderDetail 实体。
应用程序代码
用户单击**“Accept Order”(接受订单)**按钮时,即创建一个新的 SalesOrderDetail 并添加到存储中。然后处理将返回到主窗体和 buttonAddSalesOrder_Click 处理程序,在其中对数据网格进行更新。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AdventureWorksModel;
using System.Data.Objects;
namespace AdWksSalesWinDataBind
{
public partial class FormAddSalesOrderDetail : Form
{
ObjectContext parentObjCtx = null;
public FormAddSalesOrderDetail()
{
InitializeComponent();
}
public FormAddSalesOrderDetail(ObjectContext ctx)
{
InitializeComponent();
parentObjCtx = ctx;
}
private void buttonCancelOrder_Click(object sender, EventArgs e)
{
this.Dispose();
}
private void formAddSalesOrderDetail_Load(object sender, EventArgs e)
{
if (this.Owner.Controls["textBoxOrderId"].Text.Equals(""))
{
MessageBox.Show("Add Sales Order Header Number",
"No Header Number");
this.Dispose() ;
return;
}
bindingSource1.DataSource =
((AdventureWorksEntities)parentObjCtx).
Product.Execute(MergeOption.AppendOnly);
dataGridViewProducts.DataSource = bindingSource1;
}
private void buttonAcceptNewOrder_Click(object sender, EventArgs e)
{
using (AdventureWorksEntities objCtx = new AdventureWorksEntities())
{
SalesOrderDetail newSODetail = new SalesOrderDetail();
newSODetail.ProductID =
Int32.Parse(dataGridViewProducts.SelectedRows[0].Cells[0].Value.ToString());
if (textBoxQuantity.Text.Equals(""))
{
MessageBox.Show("Enter quantity");
return;
}
newSODetail.OrderQty = Int16.Parse(textBoxQuantity.Text);
try
{
newSODetail.CarrierTrackingNumber = "4E0A-4F89-AE";
newSODetail.ModifiedDate = DateTime.Now;
newSODetail.SpecialOfferID = 1;
newSODetail.rowguid = Guid.NewGuid(); // create new guid.
newSODetail.UnitPrice =
Decimal.Parse(dataGridViewProducts.SelectedRows[0].Cells[9].Value.ToString());
if (!textBoxDiscount.Text.Equals(""))
{
newSODetail.UnitPriceDiscount =
Decimal.Parse(textBoxDiscount.Text) * (Decimal).01;
}
else
{
newSODetail.UnitPriceDiscount = 0;
}
Int32 textboxHeaderNumber = Int32.Parse(this.Owner.Controls["textBoxOrderId"].Text);
ObjectParameter parameter =
new ObjectParameter("p", textboxHeaderNumber);
SalesOrderHeader header =
objCtx.SalesOrderHeader.Where(
"it.SalesOrderID = @p", parameter).
FirstOrDefault<SalesOrderHeader>();
objCtx.AddToSalesOrderDetail(newSODetail);
header.SalesOrderDetail.Add(newSODetail);
objCtx.SaveChanges();
}
catch (Exception exception)
{
MessageBox.Show("Msg: " + exception.Message + "\r\nInner Exception: " +
exception.InnerException);
}
}
}
private void textBoxDiscount_Validating(object sender, CancelEventArgs e)
{
if(textBoxDiscount.Text.Equals(""))
return;
if (Int32.Parse(textBoxDiscount.Text) < 0 ||
Int32.Parse(textBoxDiscount.Text) > 25 )
{
MessageBox.Show("Enter discount between 0 and 25");
}
}
}
}
另请参见
概念
Adventure Works 数据绑定(EDM 示例应用程序)
AdventureWorks 数据绑定示例应用程序代码(EDM 示例应用程序)