[英]How to bind two tables to Datagridview using entity framework C#
I have two Tables Estate and EstateType. 我有两个表Estate和EstateType。 How to bind those two tables to one data grid?
如何将这两个表绑定到一个数据网格? I tried something like this but this doesn't work.
我尝试过类似的操作,但这不起作用。
var getAllEst = (from ee in AgencyContext.Estate join eeT in AgencyContext.EstateType
on ee.EstateID equals eeT.EstateID select ee).ToList();
dataGridView1.DataSource = getAllEst;
Instead of string values EstateType, Landscape, Description it gives me numbers 而不是字符串值EstateType,Landscape,Description它为我提供了数字
As suggested @tpayne84 I'm added new columns but now I get this instead of values: 如建议的@ tpayne84,我添加了新列,但是现在我得到了它而不是值:
This is the code I used: 这是我使用的代码:
var getAllEst = (from ee in AgencyContext.Estate join eeT in AgencyContext.EstateType
on ee.EstateID equals eeT.EstateID select ee).ToList();
DataGridViewTextBoxColumn EstateType1 = new DataGridViewTextBoxColumn();
DataGridViewTextBoxColumn LandScape1 = new DataGridViewTextBoxColumn();
DataGridViewTextBoxColumn Description1 = new DataGridViewTextBoxColumn();
EstateType1.DataPropertyName = "EstateType";
LandScape1.DataPropertyName = "LandScape";
Description1.DataPropertyName = "Discription";
dataGridView1.Columns.Add(EstateType1);
dataGridView1.Columns.Add(LandScape1);
dataGridView1.Columns.Add(Description1);
dataGridView1.DataSource = getAllEst;
The problem looks like you are displaying the Foreign Key ID of the referenced object instead of the Property on the Object that you wish to display. 该问题似乎是在显示所引用对象的外键ID,而不是要显示的对象上的属性。
This happens because you are allowing the DataGridView to generate your columns for you. 发生这种情况是因为您允许DataGridView为您生成列。
To remedy this, you will need to create the columns yourself: 为了解决这个问题,您需要自己创建列:
DataGridViewTextBoxColumn nameColumn = new DataGridViewTextBoxColumn();
// Where "Name" is the String representation of the Property on the
// Object that you want to display.
nameColumn.DataPropertyName = "Name";
First of all, your query 首先,您的查询
var getAllEst = (from ee in AgencyContext.Estate join eeT in AgencyContext.EstateType on ee.EstateID equals eeT.EstateID select ee).ToList();
will only select AgencyContext.Estate
objects that have an associated EstateType
using an inner join...unless your Estate class
has some navigation property to the EstateType
. 只会使用内部
EstateType
选择具有关联的EstateType
AgencyContext.Estate
对象...除非您的Estate class
具有对EstateType
某些导航属性。 If you have something like: 如果您有类似的东西:
class Estate
{
// ...other properties
// I'm guessing the relationship here...
public virtual EstateType EstateType { get; set; }
}
then you would be able to access the EstateType
properties through that property. 那么您将可以通过该属性访问
EstateType
属性。
Unfortunately, DataGridView
doesn't seem to support binding to child properties so you wouldn't be able to set column.DataPropertyName = EstateType.SomeProperty
, but you could work around that by overriding EstateType.ToString()
or handling the CellFormatting
event on the DataGridView
. 不幸的是,
DataGridView
似乎不支持绑定到子属性 ,所以你不能就能够设置column.DataPropertyName = EstateType.SomeProperty
,但你可以解决,通过重写EstateType.ToString()
或处理CellFormatting
上的事件DataGridView
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.