Results 1 to 6 of 6

Thread: [RESOLVED] GridView - Show 'pretty name' of class property

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Resolved [RESOLVED] GridView - Show 'pretty name' of class property

    Hi,

    Sorry the title is a bit vague, I didn't know how to describe it.

    The problem is very simple actually: I have a class Person that represents a person in my database. This class has a CategoryId property (int) and a Category property (type Category).
    Category is a class that represents a category in my database.
    csharp Code:
    1. public class Person
    2. {
    3.     public int Id {get; set;}
    4.     public string Firstname {get; set;}
    5.     public string Lastname {get; set;}
    6.    
    7.     public int CategoryId {get; set;}
    8.     public Category Category {get; set;}
    9. }
    10.  
    11. public class Category
    12. {
    13.     public int Id {get; set;}
    14.     public string CategoryName {get; set;}
    15. }

    I am displaying a list of these Persons in a GridView, and I would like to display the name of the Category (the CategoryName property to be precise). So I define this markup;
    xml Code:
    1. <asp:GridView runat="server" ID="personsGrid" AutoGenerateSelectButton="True"
    2.             AutoGenerateColumns="False" DataKeyNames="Id">
    3.  
    4.             <Columns>
    5.                 <asp:BoundField DataField="Id" HeaderText="Id" />
    6.                 <asp:BoundField DataField="Firstname" HeaderText="Firstname" />
    7.                 <asp:BoundField DataField="Lastname" HeaderText="Lastname" />
    8.                 <asp:BoundField DataField="Category" HeaderText="Category"  />
    9.             </Columns>
    10.  
    11.         </asp:GridView>

    While this works, the Category column obviously does not display the name of the category, but rather the type name (ProjectName.Category).

    Usually I would solve this problem by one of two ways:
    1. Override the ToString method of the Category class and return the name.
    2. Add a readonly 'CategoryName' property to the Person class, where I return 'this.Category.CategoryName' (and then bind the column to this property instead).


    In this case however, I am using the Entity Framework, and the Person and Category classes are automatically generated by the database model. I suppose I could edit the generated code manually, but I don't like that, since any change in the model will cause VS to re-generate the code and my changes would be lost. So these two methods are not going to work...


    The simple question remains: how do I make the Category column show the CategoryName property of the object it represents, rather than just the type name?

    In a DropdownList for example (which I am already using for the user to select a category when creating a person), I can set the DataTextField (to "CategoryName") and DataValueField (to "Id") properties and it displays the right name and uses the right value (the Id). I can't find anything similar for a BoundField though... Am I overlooking something obvious?

    Another solution would be if I could tell the Entity Framework model to add another property to my Person class which returns the CategoryName of the Category. I can't find any way to do that though (I am a compleet noob in EF), so if anyone happens to know how to do that, that would work equally well


    Thanks!

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: GridView - Show 'pretty name' of class property

    Hey,

    I can see exactly what you want to do, but I honestly have no idea how to do it

    I don't know enough about EF to say whether you can do this, but my gut feeling is that you can. The onlt thing that I can suggest in further investigation into this area, and see what turns up.

    Sorry I can't be of more help!

    Gary

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: GridView - Show 'pretty name' of class property

    I figured it out. Turns out the entity classes (Person, Category, etc) are partial classes, so I can just extend them wherever I want. As long as I keep their names the same I can now define a CategoryName property on the Person class in a different file (which won't be destroyed by the automatic generator).

    Now you know!

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] GridView - Show 'pretty name' of class property

    Doh!!!

    For course you can!! I have totally done that in the past, on a similar project, not using EF, but we created partial classes to "amend" and auto-generated file.

    Glad to hear you got it working. Need to file this information "somewhere" so that I don't forget it

    Gary

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [RESOLVED] GridView - Show 'pretty name' of class property

    Yeah, I'm sure everyone on this forum has used this approach at least once... when creating a windows form or asp page It's exactly the same lol, the designer generates the code file that adds the controls to the form/page and sets their properties and the code you type in is the other half of the partial class Can't believe I didn't think of that sooner.

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] GridView - Show 'pretty name' of class property

    Yip, definitely a eureka moment

    Glad you got it sorted out!!

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width