This is a migrated thread and some comments may be shown as answers.

DataGridView with Entity Framework

5 Answers 668 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Manuel
Top achievements
Rank 1
Manuel asked on 02 Jul 2009, 10:17 PM
Hi:
I'm working with Entity Framework (EF), and I have 2 entities (CF_ContribucionesCuota, CF_TipoDocumento) with an association * (Many) to 0..1(Zero or One).

I'm displaying the information in a gridView as follow: (i'm using a BindingSource)
this.cF_ContribucionesCuotaBindingSource.DataSource = context.CF_ContribucionesCuota.Include("CF_TipoDocumento");
this.radGridView1.DataSource = this.cF_ContribucionesCuotaBindingSource;

In the radGridView1, shown a column call CF_TipoDocumento that is the NavigationProperty.
Well, I want to generate a GridViewComboBoxColumn in this column (CF_TipoDocumento) and the ability to change this relationship.

With ADO.net it works...
But, with Entity framework not works...
I try this:
this.cF_TipoDocumentoBindingSource.DataSource = Context.CF_TipoDocumento;

 

GridViewComboBoxColumn gvcbc = new GridViewComboBoxColumn();

 

gvcbc.FieldName =

"CF_TipoDocumento";

 

gvcbc.DataSource =

this.cF_TipoDocumentoBindingSource;

 

gvcbc.DisplayMember =

"TDT_Descripcion";

 

 

this.radGridView1.Columns.Add(gvcbc);

 



Can you help me!!

Thanks!!

5 Answers, 1 is accepted

Sort by
0
Julian Benkov
Telerik team
answered on 07 Jul 2009, 08:20 AM
Hello Manuel,

Please send us the project to reproduce the problem locally. Thank you in advance.

Sincerely yours,
Julian Benkov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Manuel
Top achievements
Rank 1
answered on 07 Jul 2009, 04:05 PM
Hello Julian:
Tell me how to send you the project..
But it is not complicate to reproduce.
1. Create a model of entity framework with 2 entities (tables) with an association * (Many) to 0..1(Zero or One).
    For example: Employees and departments, when many employees may have zero or one department.
Employees                 Departments
EmployeeID                DepartmentID
Name                          Description
DepartmentID

2. Display the information in a gridView, as follow:
this.radGridView.DataSource = context.Employee.Include("Departments").
The relationship be shown in a column of the gridView like an object that represent the department entity.
Well, I want to generate a GridViewComboBoxColumn in this column and the ability to change this relationship.

I use VS2008, and SQL server 2008
thanks!!
0
AndyRutter
Top achievements
Rank 2
answered on 08 Jul 2009, 07:15 PM
I have exactly this same issue using Countries and Regions.
Does anyone have a solution to this?
An example of using a WinForms gridview with the Entity Framework would be very useful.
At the moment it seems the Entity Framework is pretty much useless as you cannot bind entities to controls very easily..
0
Manuel
Top achievements
Rank 1
answered on 08 Jul 2009, 07:59 PM
Hi Brian,
we're working on that, and we have almost, maybe in 1 more day!!
and will share in this forum.

I also expect some comments from Telerik.

0
Julian Benkov
Telerik team
answered on 09 Jul 2009, 03:24 PM
Hi Manuel,

The problem is in Entity Framework and its object model. When you map the database objects to their foreign key constraints - in your case Employees contains Department reference object - and you add or edit a row in RadGridView, it will fail always because the change is apply to Departments.DepartmentID key but it is not applied to Employees.DepartmentID.

You can setup grid to show data but cannot provide valid add and edit operations:

private TestDBEntities1 entity; 
private void EntityForm_Load(object sender, EventArgs e) 
    entity = new TestDBEntities1(); 
    this.radGridView1.DataSource = entity.Employees; 
    this.radGridView1.Columns.RemoveAt(2); 
    GridViewComboBoxColumn comboColumn = new GridViewComboBoxColumn("Departments.DepartmentID""Departments.DepartmentID"); 
    comboColumn.DataSource = entity.Departments; 
    comboColumn.ValueMember = "DepartmentID"
    comboColumn.DisplayMember = "Description"
    this.radGridView1.Columns.Add(comboColumn); 
 
private void radButton1_Click(object sender, EventArgs e) 
    entity.SaveChanges(); 


To support all RadGridView functionality in this case you have to remove all constraints from your database objects and make new entity model without constraints. Then you can map to the DepartmentID field:

private TestDBEntities1 entity; 
private void EntityForm_Load(object sender, EventArgs e) 
    entity = new TestDBEntities1(); 
    this.radGridView1.DataSource = entity.Employees; 
    this.radGridView1.Columns.RemoveAt(2); 
    GridViewComboBoxColumn comboColumn = new GridViewComboBoxColumn("DepartmentID""DepartmentID"); 
    comboColumn.DataSource = entity.Departments; 
    comboColumn.ValueMember = "DepartmentID"
    comboColumn.DisplayMember = "Description"
    this.radGridView1.Columns.Add(comboColumn); 
 
private void radButton1_Click(object sender, EventArgs e) 
    entity.SaveChanges(); 

For the next version of Entity framework this problems will be resolved. You can read the following blog post.

Kind regards,
Julian Benkov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
GridView
Asked by
Manuel
Top achievements
Rank 1
Answers by
Julian Benkov
Telerik team
Manuel
Top achievements
Rank 1
AndyRutter
Top achievements
Rank 2
Share this question
or