When I have to populate a dgv with only 1 table it's ok, I know how to do it (List< Entity > coming from database to datasource). But when I have a table that has a FK with another one, I don't know how to show another property of that table (and not the ID, that the user wont understand). I think what I've done with LINQ is really bad (BTW I'm new to LINQ, I don't know if I need it, at least here). The problem with my solution comes when I try to get the selected ID from the DGV.
private void LoadDGV()
{
BuildingLogic oBuildingLogic;
try
{
oBuildingLogic = new BuildingLogic();
SocietyLogic oSocietyLogic = new SocietyLogic();
List<Building> listBuildings = oBuildingLogic.GetAll();
List<Society> listSocieties = oSocietyLogic.GetAll();
this.dgvBuildings.DataSource = (from building in listBuildings
join society in listSocieties
on building.IDSociety equals society.ID
select new { building.ID, FullName = building.FullName, ShortName = building.ShortName, Society = society.Name, Date = building.Date, City = building.City, Address = building.Adress }).ToList();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
oBuildingLogic = null;
}
}
private int ItemSelectedID()
{
if (this.dgvBuildings.SelectedRows.Count > 0)
{
//something like this?
//this.dgvBuildings.SelectedRows[0].DataBoundItem;
}
else
{
return 0;
}
}
return Convert.ToInt32(this.dgvBuildings.SelectedRows[0].Cells[0].Value);
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.