locked
How display the related description in a column that has a relationship to a table of foreign keys? RRS feed

  • Question

  • User1679353836 posted

    Hello All:

    Please be patient, I am new to this forum, Dynamic Data Services and Entity Framework.

     I have a Dynamic Data Entity website. I have 3 tables ("tableA", "tableB", "tableC").

    "tableA" is related to "tableB" whose columns are only foreign keys. "tableB" has a relationship with "tableC". Inside "tableC" we have a column with a description. I need to display that description "tableC.Description" in my datagrid Column for "tableA".

    Currently the datagrid Column displays only the integer value. "tableB" datagrid displays the description from the related table "tableC", as it should.

     I have reviewed the following post: "How to get a datagrid to display data associated with a foreign key " and it does not apply to my case.

     I appreciate your time and patience.

    Friday, December 12, 2008 5:01 PM

Answers

  • User1679353836 posted

    In the end, we followed the example from the below post to override the ToString()  and took it a step further as follows:

    <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>

    http://blogs.msdn.com/rickandy/archive/2008/11/22/improving-the-fk-field-display-showing-two-fields-in-foreign-key-columns-with-ef.aspx

    <o:p> </o:p><o:p>

           public override string ToString(){    

                //MasterTablePointerID

                System.Data.Objects.DataClasses.EntityReference mtp = MasterTablePointerReference;

                int mtpValue = (int)mtp.EntityKey.EntityKeyValues[0].Value;

               

                //UseCategoryID

                System.Data.Objects.DataClasses.EntityReference uc = UseCategoryReference;

                int ucValue = (int)uc.EntityKey.EntityKeyValues[0].Value;

               

                var DC = new LM_BuildingEntities();

                var mtp_uc_desc = (from bm in DC.BuildingMaster

                                   where bm.MasterTablePointer.MasterTablePointerID == mtpValue && bm.UseCategory.UseCategoryID == ucValue

                                   select new{desc = bm.MasterTablePointer.Name + " + " + bm.UseCategory.Name}).FirstOrDefault();

                return mtp_uc_desc.desc;

            }

    </o:p>Thank you for your time and suggestions. [:D]

    <o:p> </o:p>

    Regards.

    <o:p> </o:p>

    Barbara

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 16, 2008 3:58 PM

All replies

  • User-330204900 posted

    Hi Bjrivera, My first thought would be to create my own custom FieldTemplate and use some Linq to Entities to reach out an get the Description field table.

    Have a look at these articles to get you started on custom FieldTemplates:

    1. The Anatomy of a FieldTemplate.
    2. Your First FieldTemplate.
    3. An Advanced FieldTemplate.
    4. A Second Advanced FieldTemplate.
    5. An Advanced FieldTemplate with a GridView ***UPDATED ***.
    6. An Advanced FieldTemplate with a DetailsView ***UPDATED ***.

    Hope this helps [:D]

    Friday, December 12, 2008 7:26 PM
  • User1679353836 posted

    Thank you for your response and your suggestions. 

    I would like to use DisplayColumn(" ") to handle this scenario by passing the "Description" as the string in DisplayColumn(" ") or an alternate string to accomplish the same.

     Please let me know your thoughts.

     

    Monday, December 15, 2008 8:44 AM
  • User-330204900 posted

    this won't work out of the box you will need a custom FieldTemplate and use UIHintAttribute to tell Dynamic Data to use your custom FieldTemplate. You can then use Linq to SQL to get at the values in the related table.

    Monday, December 15, 2008 9:16 AM
  • User1679353836 posted

    Hi Steve,  

    I am wanting to avoid any customization and to be able to extend the DisplayColumnAttribute to the next table.

    As a review and some clarity for myself:

    I have 3 tables: 

    1) TableA -TableAID, TableBID 

    2) TableB - TableBID, TableCID

    3) TableC - TableCID, Description

    I want to extend the DisplayColumnAttribute to the next table to get the needed description for this example; DisplayColumn("TableA.TableB.TableC.Description", "", ).  

    I appreciate your time and patience.

    Barbara Rivera

    Monday, December 15, 2008 10:37 AM
  • User1679353836 posted

    Hi Steve,

    Just thinking, if extending the DisplayColumnAttribute is not possible, how about creating a Custom Attribute?

    Thanks.

    Barbara

    Monday, December 15, 2008 11:01 AM
  • User-330204900 posted

    As I said in earlier what you need is a custom FieldTemplate and use the UIHintAttribute to set the column you want to display the value again I'm asuming if the FK column on TableA. [:D]

    Monday, December 15, 2008 2:06 PM
  • User1679353836 posted

    In the end, we followed the example from the below post to override the ToString()  and took it a step further as follows:

    <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>

    http://blogs.msdn.com/rickandy/archive/2008/11/22/improving-the-fk-field-display-showing-two-fields-in-foreign-key-columns-with-ef.aspx

    <o:p> </o:p><o:p>

           public override string ToString(){    

                //MasterTablePointerID

                System.Data.Objects.DataClasses.EntityReference mtp = MasterTablePointerReference;

                int mtpValue = (int)mtp.EntityKey.EntityKeyValues[0].Value;

               

                //UseCategoryID

                System.Data.Objects.DataClasses.EntityReference uc = UseCategoryReference;

                int ucValue = (int)uc.EntityKey.EntityKeyValues[0].Value;

               

                var DC = new LM_BuildingEntities();

                var mtp_uc_desc = (from bm in DC.BuildingMaster

                                   where bm.MasterTablePointer.MasterTablePointerID == mtpValue && bm.UseCategory.UseCategoryID == ucValue

                                   select new{desc = bm.MasterTablePointer.Name + " + " + bm.UseCategory.Name}).FirstOrDefault();

                return mtp_uc_desc.desc;

            }

    </o:p>Thank you for your time and suggestions. [:D]

    <o:p> </o:p>

    Regards.

    <o:p> </o:p>

    Barbara

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 16, 2008 3:58 PM