简体   繁体   English

C#datatable实体框架

[英]C# datatable Entity Framework

I have two objects people and house , and I want to show the table people in a datagridview, but in the field House , the value shown is Program.Model.House ; 我有两个对象people house ,我想在datagridview中显示表人,但在字段House ,显示的值是Program.Model.House ; I want to show the street name instead, how I can do it? 我想改为显示街道名称,我该怎么做? (I'm using Entity Framework and the data source of data grid view I use context.tolist() ) (我正在使用Entity Framework和数据网格视图的数据源我使用context.tolist()

public class People 
{
    public string Name { get; set; }
    public House House { get; set; }
}

public class House
{
    public string Street { get; set; }
    public int Number { get; set; }
}

Override ToString() in House class. 在House类中覆盖ToString()。 Like that : 像那样 :

public override string ToString(){ return $"Street: {Street}"; }

You wanted to show data like Name and Street Name 您想要显示NameStreet Name等数据

For Name You can use Model.Name 对于名称您可以使用Model.Name

For Street Name you can use Model.House.Street 对于街道名称,您可以使用Model.House.Street

For Example 例如

 @foreach(var item in Model)
 {
       <td> @Model.Name</td>
       <td> @Model.House.Street</td>
 }

May this help you 愿这对你有所帮助

Simply create a new object for grid view with a street name. 只需使用街道名称为网格视图创建一个新对象。

List<object> peopleData=new List<object>(); 
peopleData.Add(new{ ppl.Name,ppl.House.Street });

grdPeople.DataSource = peopleData;```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM