none
EF4 - binding complex types to datagridview columns RRS feed

  • Question

  • Hi all,

    Using C#, VS 2010, I have an EF model that uses complex types.  I did this simply because I have a similar set of values in multiple tables (ID of person entering the data, date the data was entered, persons dept ID, persons rank, etc.)  Since I have to track this info for many tables, I figured a complex type would provide some benefit.

    So now I have a result set that I want to show in a datagridview.  If I assign the result set to the DataSource, when I get to the complex type, it just shows the name of the complex type. 

    So I figured I would hide that column and add a few to hold those values - all easy to do.  Until I tried to bind the data to those columns.  Nothing I try seems to work. 

    Given a complex type named "EntryDetail" which has userDetail (which in turn has userId, rank, and deptId) entryDetail (which in turn has entrydate and entryType), how would I bind those values to some new columns I added?

    For the userId column I added, I have tried:

    System.Windows.Forms.DataGridViewTextBoxColumn newCol1 = new System.Windows.Forms.DataGridViewTextBoxColumn();

    newCol1.DataPropertyName ="userId"

    newCol1.DataPropertyName ="EntryDetail.userDetail.userId"

    newCol1.DataPropertyName ="{EntryDetail.userDetail.userId}"

    newCol1.DataPropertyName ="{Binding EntryDetail.userDetail.userId}"

    newCol1.DataPropertyName ="{userId}"

    And anything else I could think of to do.  But nothing works.  If I reference a scalar property, it works just fine. 

    What am I missing???

    And yes, I did spent a bunch of time searching, but for whatever reason, couldn't find an answer to this...

    Final thought - I am sure I can just add properties to the EF class that would expose these as scalar types to my code, but then why use a complex type in the first place?  Or I can just eliminate the whole complex type approach as a failure and just make everything a scalar type.  If these are my only options, any thought on one over the other?

    Thanks


    me (and yes, I DO mark correct answers)

    Monday, December 10, 2012 9:19 PM

Answers

    1. In your "model" folder create a class and put one property in it like this:
    ObservableCollection<EntryDetail> MyEntityDetailModel {get;set;}

    Then follow the directions above... This will allow you to drag a datagrid or listview or details view automatically.  Go ahead and spend the time to learn it because you will love this feature down the road.

    JP Cowboy Coders Unite!

    • Marked as answer by Alexander Sun Thursday, December 20, 2012 7:58 AM
    Saturday, December 15, 2012 3:15 AM

All replies

  • Here's a trick I use often.  In EF, the objective is to have classes that represent data schema.  But you can also create a model of that data as an object of which you can add as a datasource!  The nice thing about doing that is that the designer will create all the fields for you in the datagrid!  Takes about 2 minutes to do once you figure it out.

    1. Step one, create a folder named model and add a class to it that has just one property like this:
    ObservableCollection<MyEfComplexClass> MyEFModel {get;set;}

    Now you want to be able to create a data source based on that class object.  In VS2012 goto View/Other Windows/Data Sources. Right click to add a new Data Source and then pick Object.  This allows you to find this class you just created.

    Bring up a User Control or Window and then right click on the new data source.  You get to pick how you want it displayed as a DataGrid, List or Details, select the one you want and then drag it into your user control.

    You're done!


    JP Cowboy Coders Unite!

    Tuesday, December 11, 2012 12:00 AM
  • Ok, thanks for the response.  I am not quite sure what you want me to create or the appropriate syntax.

    Give the example I used, would I replace <MyEfComplexClass> with <EntryDetail>?  I assume the name of the property isn't relevant.

    I will try it, but not sure how it will end up exposing the details of the EntryDetails.userDetail.userId value and see what happens.

    Thanks


    me (and yes, I DO mark correct answers)

    Friday, December 14, 2012 7:45 PM
    1. In your "model" folder create a class and put one property in it like this:
    ObservableCollection<EntryDetail> MyEntityDetailModel {get;set;}

    Then follow the directions above... This will allow you to drag a datagrid or listview or details view automatically.  Go ahead and spend the time to learn it because you will love this feature down the road.

    JP Cowboy Coders Unite!

    • Marked as answer by Alexander Sun Thursday, December 20, 2012 7:58 AM
    Saturday, December 15, 2012 3:15 AM