直接在 DataPropertyName 上設定 CustName 啦
Order Class
namespace DataGridView4PartialClass
{
using System;
using System.Collections.Generic;
public partial class Order
{
public int OrderID { get; set; }
public System.DateTime OrderDate { get; set; }
public int CustID { get; set; }
public virtual Customer Customer { get; set; }
}
}
Partial Classnamespace DataGridView4PartialClass
{
public partial class Order
{
public string CustName
{
get
{
if (this.Customer != null)
{
return this.Customer.CustName;
}
else
{
return string.Empty;
}
}
}
}
}
C# Codeusing System.Data.Entity;
namespace DataGridView4PartialClass
{
public partial class Form1 : Form
{
EFDbContext context;
BindingSource bsOrder;
public Form1()
{
InitializeComponent();
context = new EFDbContext();
bsOrder = new BindingSource();
}
private void Form1_Load(object sender, EventArgs e)
{
context.Order.Include(c => c.Customer).Load();
bsOrder.DataSource = context.Order.Local.ToBindingList();
dgvOrders.AutoGenerateColumns = false;
dgvOrders.DataSource = bsOrder;
}
}
}
執行顯示結果在該篇文章內看到 DataGridView 要顯示導覽屬性的 6 種方式
- 使用 DataGridViewComboBoxColumn
- 使用 Partial Class
- 使用 LINQ
- 使用 DataGridView 的 CellFormatting Event
- 覆寫並透過 ToString()
- 自訂 TypeDescriptor
沒有留言:
張貼留言