Page 1 of 1

Using Entities classes with DataGridView

Posted: Thu 12 Mar 2009 00:54
by mpovidlov
It is very easy to display and edit a database table represented by Entity class in DataGridView:

Code: Select all

var context = new Entities();
dataGridView1.DataSource = context.EMPLOYEE;
But when one of the columns is an FK to another table (like Department) the DataGrid would display the whole Entity Department in that column. As a result it does not show anything. I am interested in the FK value (DepartmentID) which is still a part of Employee table. Is it possible to tell the DataGrid to display the DepartmentID instead of the whole Department?
Thanks

Posted: Thu 12 Mar 2009 15:43
by AndreyR
First of all, I advise you to include Department objects like in the following example:

Code: Select all

dataGridView1.DataSource = context.emp.Include("dept");
This will load Department object associated with Employee.
As for your question about the good way to display the loaded objects, unfortunately, it exceeds the bounds of our support.