Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi all.
I have a question for you regarding EntityFrameWork and DataGridView. Actually this Q is more generic because it would be the same if using EntityFramework, DataSet/DataTable, Linq2Sql, etc. etc.
Ok. Here goes:
I have a DataGridView. This DataGridView is filled with data from a resultset of a LINQ request, something like this:
var records = from r in db.Customers
    select new
    {
        Name = r.Name,
        Address = r.Address,
        rec = r
    }
dgvCustomers.DataSource = records;
dgvCustomers.Columns["rec"].Visible = false;

now, this works wery well and the code is easy to type and read. Proble,, is this: The record bound to the DGV has no reference to the record in the db.Customers. Allso, the fields retreived fro, the select is standard STRINGs with no reference to the rec that allso is retreived.
This means that updating the rec wil not update the DGV. Updating the fields in the DGV wil not update the REC etc. etc.

One way to work around this that I use a lot is wrapping it all in a record class:
var records = from r in db.Customers
    select r;
List<customerrecord> customers = new List<customerrecord>()
foreach(var r in records)
  customers.Add(new CustomerRecord(r));
</customerrecord></customerrecord>

And this lets me control everything in the record since I can have properties in the record class.

Now, I have played a bit with EntityFrameWork and I get the feeling that this approach is kind of obsolete. That the EntityFramework contains all I nead to do this.

Allso, when searchning the MSDN I see that they allways use a BindingSource instead of binding the result directly to the DataSource. Should I use a BindingSource instead? And if so - Why?
Posted
Comments
vivekse 30-Dec-10 3:38am    
+5 for nicely discuss your problem.

When used with a collection a BindingSource supports navigation through it's notion of a "Current"[^] item.

BindingSource can also be used to create master/detail bindings in a simple manner where the detail BindingSource is bound to a collection property of the element type of the collection bound to the "master" BindingSource using the DataMember property of the "detail" BindingSource.

Entity framework generates collection properties for one-to-many relations.

Regards
Espen Harlinn
 
Share this answer
 
Comments
Dalek Dave 5-Jan-11 3:34am    
Good clear answer.
I'm not sure how this wil help? My question was "Whats best practice regarding DataGridView and Entity Framework".
 
Share this answer
 
I'm not sure how this wil help? My question was "Whats best practice regarding DataGridView and Entity Framework".
 
Share this answer
 
Comments
Espen Harlinn 4-Jan-11 11:46am    
IMHO: Using a BindingSource is "Best Practice" when working with windows forms.
Formulate your linq query expressions so that the result is of System.Data.Objects.ObjectSet. Usually achieved by not doing anything fancy.
Ok. I might have misunderstood you.

But now we'r talking. This is probably just what I'm looking for.

Lets say the DGV form have one DataGridView holding customers. Allso, lets assume the customer record on the SQL server is huge, as in containing 50 fields. Only 3 of them is neaded in the listbox. What would be the correct way to get the dataset?

Could you please give me an example of the abowe, and allso - what code would you put on the ADD and CHANGE button. I assume that this code will send the record as a reference so how the form is handled I think I understand.


All must be using Entity Framework of course...

Thanx in advanced...

Regards
Ole
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900