简体   繁体   English

如何使用实体框架C#将两个表绑定到Datagridview

[英]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.

相关问题 取消绑定并再次绑定datagridview C#和Entity Framework - Unbind and bind again a datagridview C# & Entity Framework 将数据从Entity Framework模型绑定到DataGridView C# - Bind data from Entity Framework model to DataGridView C# 如何使用Entity Framework将数据从多个表绑定到datagridview并使用CRUD操作? - How to bind data from mutiple tables to datagridview using Entity Framework and use CRUD operations? 如何使用实体框架在WPF c#中的两个表之间插​​入一对多关系的数据? - How to insert data for one to many relationship between two tables in WPF c# using Entity Framework? 使用实体框架从两个表中绑定数据网格视图 - Bind Data Grid View from two tables using Entity framework 如何使用实体框架在一个dataGridView中合并多个表? - How to combine multiple tables in one dataGridView using Entity Framework? 在Crystal Report Entity Framework C#中联接两个表 - Join two tables in crystal report Entity Framework c# 无法链接两个表; MySQL的; 实体框架C# - Cannot link two tables; mysql; entity framework c# 使用Entity Framework在单个Button中单击将数据插入两个不同的表中,在c#中单击 - Insertion of data in two different tables using Entity Framework within single Button click in c# C#DataGridView不显示数据(实体框架) - C# DataGridView not showing Data (Entity Framework)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM