Smartboy
Smartboy

Reputation: 984

Data grid View ( winform ) not populating properly

I have two tables in my database Department and Faculty . I am populating a datagridview for department list in my winform application . Both of my tables are following

Department

  1. Sno (int)
  2. Name (varchar(max))
  3. Status (bit)
  4. Faculty_id (int)

Faculty

  1. Sno (int)
  2. Name
  3. Status

Now come to code i simply drag and drop a datagridview into my form and go to code behind file . In my form's load method i write following code

        var main = new SRMEntities();
        var departs = main.Department.ToList();
        DepartmentGrid.DataSource = departs;

now when my form load it show like this Department Grid

Look in faculty column its showing nothing and when we create department we store a faculty id into department . i want to show faculty name here .

Please let me know if you have any question .

Thanks in advance

Edit

My Database Diagram

Database

Upvotes: 0

Views: 461

Answers (3)

BizApps
BizApps

Reputation: 6130

You need to join your tables

something like:

var main = new SRMEntities();

    var source = (from d in main.Department
                 join f in main.Faculty on d.Faculty.Sno=f.Sno
                 select new
                 {
                  Sno = d.Sno ,
                  Name = d.Name,
                  Status = d.Status, 
                  Faculty = f.Name
                 }
                 );   

    DepartmentGrid.DataSource = source.ToList();

Regards

Upvotes: 0

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236318

Here is correct join:

var main = new SRMEntities();
var query = from f in main.Faculty
            join d in main.Department on f.Sno equals d.Faculty.Sno
            select new { d.Sno, d.Name, d.Status, Faculty = f.Name };

DepartmentGrid.DataSource = query.ToList();

Upvotes: 2

andy
andy

Reputation: 6079

Everything is dependent on your query............ Your query should contain both the tables with Join.

var main = new SRMEntities();
var departs = main.Department.ToList();
DepartmentGrid.DataSource = departs;

Please check the above code "main.Department.ToList();" is returning only Departments and your assigning the "departs" to your grid.

Whether "main.Department" contains the "Faculty" table info?

By looking at your code i am guessing you have a seperate items like below

1.main.Department
2.main.Faculty

Upvotes: 1

Related Questions