Populate PanelBar using List

6 posts, 2 answers
  1. Scott Cullen
    Scott  Cullen avatar
    9 posts
    Member since:
    Jan 2009

    Posted 26 Aug 2009 Link to this post

    Hi,

    I've run across a problem when populating a Panel bar using a List as the datasource. The problem occurs when I try to set the DataTextField property of the PanelBar to the property of a child object which resides in my List. Here is a code sample (btw, I am creating the List from a Linq query, but I don't think that should matter):

                    <telerik:RadPanelBar ID="MenuPanelBar" Skin="WebBlue" Runat="server"
                    </telerik:RadPanelBar> 

    LayoutFrameworkDataContext dc = new LayoutFrameworkDataContext();

    MenuPanelBar.DataSource = 
                 (from mi in dc.MenuItems 
                 from mid in dc.MenuItemDisplayNames 
                 where mi.DisplayNameID == mid.DisplayID 
                 select mi).ToList(); 
     
    MenuPanelBar.DataTextField = "MenuItemDisplayName.DisplayName"
    MenuPanelBar.DataBind(); 

    When I call DataBind() I receive this error: "Object of type Menu.MenuItem does not have a MenuItemDisplayName.DisplayName property."

    When I look at the PanelBar DataSource in the debugger I can see that I do indeed have my array of MenuItem objects, and within each MenuItem object there is a MenuItemDisplayName object with a DisplayName property.

    It's seems what I'm trying to do is pretty straight forward and when I try this with a RadGrid the code works fine and I do not receive any errors.

    Does anyone have an idea of what I may be doing wrong?

    Thanks, -Scott


  2. Answer
    Veselin Vasilev
    Admin
    Veselin Vasilev avatar
    2992 posts

    Posted 27 Aug 2009 Link to this post

    Hi Scott Cullen,

    I think the problem is caused by the fact that in the LINQ query you select mi which is of type MenuItems, but you are trying to bind to the DisplayName field which does not belong to the MenuItems class - it belogs to the
    MenuItemDisplayName class. So, you might want to select mid instead of mi:

    LayoutFrameworkDataContext dc = new LayoutFrameworkDataContext(); 
     
    MenuPanelBar.DataSource =  
                 (from mi in dc.MenuItems  
                 from mid in dc.MenuItemDisplayNames  
                 where mi.DisplayNameID == mid.DisplayID  
                 select mid).ToList();  
      
    MenuPanelBar.DataTextField = "DisplayName";  
    MenuPanelBar.DataBind(); 

    Let me know if this helps.

    Best wishes,
    Veselin Vasilev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. Scott Cullen
    Scott  Cullen avatar
    9 posts
    Member since:
    Jan 2009

    Posted 27 Aug 2009 Link to this post

    Hi Veselin,

    Thank you for your reply.

    Yes, what you posted will definitely work, however I'm under the impression that I should be able to access the property of a child object using the dot notation. This works for the RadGrid, but not for the PanelBar. I used this link as a reference: Binding to SubObjects

    I would do what you said, but I actually need to access properties in both MenuItem and MenuItemDisplayName in order to set the fields of my PanelBar, but I only used MenuItemDisplayName in my example for simplicity.

    In the example in my previous post my Linq query provides me with a List that has a (simplified) hierarchy that looks like this:

    MenuItem Object 
            | 
            |--------MenuItemDisplayName Object 
                                          | 
                                          |------DisplayName property 


    It seems as if I should be able to write "MenuItemDisplayName.DisplayName" to access that property in the child object, the same way I can for a RadGrid.


    I have also tried the following without success (in this example neither property can be found when used with a PanelBar), however I can see both the MenuItem and MenuItemDisplayName objects and they do contain the properties I want.

                MenuPanelBar.DataSource = 
                 (from mi in dc.MenuItems 
                  from mid in dc.MenuItemDisplayNames 
                  where mi.DisplayNameID == mid.DisplayID 
                  select new { mi, mid }).ToList(); 
                   
                MenuPanelBar.DataTextField = "MenuItemDisplayName.DisplayName"
                MenuPanelBar.DataFieldID = "MenuItem.ItemID"
                MenuPanelBar.DataBind(); 

    Thanks very much for your help.

    -Scott
  4. Answer
    Veselin Vasilev
    Admin
    Veselin Vasilev avatar
    2992 posts

    Posted 02 Sep 2009 Link to this post

    Hi Scott Cullen,

    Here is a slightly different approach you can try:

                NorthwindDataContext dc = new NorthwindDataContext(); 
                var data = from product in dc.Products 
                           from category in dc.Categories 
                           where product.CategoryID == category.CategoryID 
                           select new { product.ProductName, category.CategoryName }; 
     
                RadPanelBar1.DataSource = data.ToList(); 
                RadPanelBar1.DataTextField = "ProductName"
                RadPanelBar1.DataValueField = "CategoryName"
                RadPanelBar1.DataBind(); 

    Note that the anonymous class contains all the properties I would need later, so I can access them directly.

    Hope this helps.

    Greetings,
    Veselin Vasilev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  5. Scott Cullen
    Scott  Cullen avatar
    9 posts
    Member since:
    Jan 2009

    Posted 02 Sep 2009 Link to this post


    Thanks Veselin, that worked perfectly. I'm still a little confused about why I can't use the dot notation to access the fields the way I tried to in my first example query, especially since it works when used with a Grid. Do you think this is by design?

    Thanks, -Scott
  6. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 03 Sep 2009 Link to this post

    Hi Scott Cullen,

    Yes, this is by design. RadPanelBar expects the name of the property.

    Regards,
    Atanas Korchev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top