This is a migrated thread and some comments may be shown as answers.

Populate PanelBar using List

5 Answers 149 Views
PanelBar
This is a migrated thread and some comments may be shown as answers.
Scott Cullen
Top achievements
Rank 1
Scott Cullen asked on 26 Aug 2009, 10:52 PM
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


5 Answers, 1 is accepted

Sort by
0
Accepted
Veselin Vasilev
Telerik team
answered on 27 Aug 2009, 01:56 PM
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.
0
Scott Cullen
Top achievements
Rank 1
answered on 27 Aug 2009, 03:21 PM
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
0
Accepted
Veselin Vasilev
Telerik team
answered on 02 Sep 2009, 08:36 AM
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.
0
Scott Cullen
Top achievements
Rank 1
answered on 02 Sep 2009, 04:06 PM

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
0
Atanas Korchev
Telerik team
answered on 03 Sep 2009, 06:51 AM
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.
Tags
PanelBar
Asked by
Scott Cullen
Top achievements
Rank 1
Answers by
Veselin Vasilev
Telerik team
Scott Cullen
Top achievements
Rank 1
Atanas Korchev
Telerik team
Share this question
or