星期三, 2月 20, 2019

[EF] 透過導覽屬性來取出相關資料

在 Winform 上透過 TextBox.DataBinding 搭配導覽屬性,直接呈現相關資料

專案內容


C# 相關語法
using System.Data.Entity;
using WinFormsWithNavigation.Models;

namespace WinFormsWithNavigation
{
    public partial class Form1 : Form
    {
        private NavigationContext _context;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            _context = new NavigationContext();
            _context.Order.Load();

            bsOrder.DataSource = _context.Order.Local.ToBindingList();
            txtOrderID.DataBindings.Add("Text", bsOrder, "OrderID");
            txtOrderDate.DataBindings.Add("Text", bsOrder, "OrderDate");
            txtCustID.DataBindings.Add("Text", bsOrder, "CustID");
            txtCustName.DataBindings.Add("Text", bsOrder, "Customer.CustName");
            bindingNavigator1.BindingSource = bsOrder;
        }
    }
}
執行結果

沒有留言:

張貼留言