AdventureWorks 数据绑定示例应用程序代码(EDM 示例应用程序)

在实体数据模型 (EDM) 中,AdventureWorks 数据绑定应用程序包含两个用于加载和修改数据的事件处理程序。当用户单击 buttonGetHeader 时,将运行加载 SalesOrderHeader 及其关联 SalesOrderDetail 实体的代码。对 SalesOrderDetail 实体所包含数据的修改在订单详细信息网格事件处理程序 cellEndEdit 中提交。

应用程序代码

AdventureWorks 数据绑定应用程序中使用的 Windows 主窗体的代码如下所示:

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 Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
            objCtx = new AdventureWorksEntities();
            
        }

        private void buttonGetHeader_Click(object sender, EventArgs e)
        {
            if (textBoxOrderId.Text.Equals(""))
            {
                MessageBox.Show("Add Sales Order Header Number", 
                    "No Header Number");
                return;
            }
            ObjectParameter parameter =
                new ObjectParameter("p", Int32.Parse(textBoxOrderId.Text));

            dataGridViewSOHeader.DataSource = objCtx.SalesOrderHeader.Where(
                "it.SalesOrderID == @p", parameter).Execute(MergeOption.OverwriteChanges);

            if(objCtx.SalesOrderHeader.Where(
                "it.SalesOrderID == @p", parameter).Any())
            {
                SalesOrderHeader resultHeader =
                    objCtx.SalesOrderHeader.Where(
                    "it.SalesOrderID == @p", parameter).First();

                resultHeader.SalesOrderDetail.Load();
                dataGridViewOrderDetails.DataSource = resultHeader.SalesOrderDetail;

            }
            else
                MessageBox.Show(
                    "No Sales Order Header matches this key", 
                    "No Match");
            
        }

        private void Form1_FormClosing(object sender, 
            FormClosingEventArgs e)
        {
            objCtx.Connection.Close();
            objCtx.Dispose();
        }

        private void dataGridViewSOHeader_CellEndEdit(object sender,
            DataGridViewCellEventArgs e)
        {
            objCtx.SaveChanges();
            
        }

        private void dataGridViewOrderDetails_CellEndEdit(object sender,
            DataGridViewCellEventArgs e)
        {
            objCtx.SaveChanges();

            ObjectParameter parameter =
                new ObjectParameter("p", Int32.Parse(textBoxOrderId.Text));

            SalesOrderHeader header =
                objCtx.SalesOrderHeader.Where(
                "it.SalesOrderID == @p", parameter).FirstOrDefault();

            // Refresh the SalesOrderHeader in the grid.
            objCtx.Refresh(RefreshMode.StoreWins, header);

        }

        private void buttonAddSalesOrder_Click(object sender, EventArgs e)
        {  
            FormAddSalesOrderDetail formAddSODetail = new FormAddSalesOrderDetail(objCtx);
        
            DialogResult dlgResult = formAddSODetail.ShowDialog(this);

            if(dlgResult.Equals(DialogResult.OK))
            {
                ObjectParameter parameter =
                new ObjectParameter("p", Int32.Parse(textBoxOrderId.Text));

                SalesOrderHeader header =
                    objCtx.SalesOrderHeader.Where(
                    "it.SalesOrderID == @p", parameter).FirstOrDefault();

                // Refresh the SalesOrderDetails in the grid.
                buttonGetHeader_Click(null, null);
                
            }

        }

        private void textBoxOrderId_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode.Equals(Keys.Return))
                buttonGetHeader_Click(this, System.EventArgs.Empty);

        }

        
        
    }
}

另请参见

概念

Adventure Works 数据绑定(EDM 示例应用程序)
AdventureWorks 数据绑定示例添加 SalesOrderDetail 窗体(EDM 示例应用程序)