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
; 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()
)
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. Like that :
public override string ToString(){ return $"Street: {Street}"; }
You wanted to show data like Name
and Street Name
For Name You can use Model.Name
For Street Name you can use 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;```
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.