0

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();
        }
     }

'''

1
  • You need to use JOIN to select data from two tables using entity framework. You can learn about JOIN in entity framework at entityframework.net/joining Commented Jul 6, 2020 at 14:06

2 Answers 2

0

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();

OR Documentation

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 -

How to add table in Entity Framework?

Sign up to request clarification or add additional context in comments.

Comments

-1

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.

3 Comments

A link to a solution is welcome, but please ensure your answer is useful without it: add context around the link so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. Answers that are little more than a link may be deleted.
Please check the target page is available. without check we don't answer.
It doesn't matter if the target page is available today. It may not be available in the future. Read my comment again. A link to another site alone is NOT an acceptable answer here. Follow the links I gave you and read them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.