I am working on a Windows Forms Application and am using C#, entity framework. i have two tables in database battalion and brigtype. REALATION BETWEEN THEM USING ID.
i am trying to show those two tables in data grid view but one of them only appears. Here is the code I tried: '''
private void PopulateDataGridView()
{
using (Data.battdbEntities db = new Data.battdbEntities())
{
dgv.AutoGenerateColumns = true;
dgv.DataSource = db.battalions.ToList();
}
}
'''
Currently you are only binding battalions
to DataSource
. In order to have brigtype
data on the relation ID as you mentioned you need to join them on ID column:
dataGridView1.DataSource = (from s in db.battalions
join c in db.brigtypes on s.ID equals c.ID
//where condtion if any
select new
{
//Column list here
}).ToList();
dataGridView1.DataSource = db.battalions.Join(
db.brigtypes,
battalion => battalion.ID,
brigtype => brigtype.ID,
(battalion, brigtype) => new
{
//Your columns here
}).ToList();
If your DBContext battdbEntities
has not added the brigtypes
then you have to add that table also to context some thing like below -
Why don't you try to make View and bind the data in gridview.
https://www.w3resource.com/sql/creating-views/create-view-with-join.php
Try this.
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.