Formulario Add SalesOrderDetail del ejemplo AdventureWorks Data Binding (aplicación de ejemplo de EDM)

La aplicación de enlace de datos de AdventureWorks utiliza un formulario Windows Forms independiente para agregar una nueva entidad SalesOrderDetail.

Código de la aplicación

Cuando el usuario hace clic el botón Accept Order, se crea un nuevo SalesOrderDetail y se agrega al almacenamiento. El proceso vuelve a continuación al formulario principal y al controlador buttonAddSalesOrder_Click donde las cuadrículas de datos se actualizan.

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");
            }
            
        }

    }
}

Vea también

Conceptos

Adventure Works Data Binding (aplicación de ejemplo de EDM)
Código de la aplicación de ejemplo AdventureWorks Data Binding (aplicación de ejemplo de EDM)