+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Category classification as column in RadGrid

Category classification as column in RadGrid

10 posts, 0 answered
  1. Men
    Men avatar
    8 posts
    Registered:
    28 Mar 2014
    28 Mar 2014
    Link to this post

    Hi,

    I have created a RadGrid which loads items from the Sitefinity events module. In this module every item has a category. Now I want to display the name of the category (e.g. Mouse) in a column in the RadGrid.

     This doesn't work, the following is displayed: "Telerik.OpenAccess.TrackedList`1[System.Guid]" instead of the name of the category.

    How can I display the name of the category for an item in a new column?

    Thanks!

  2. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    29 Mar 2014 in reply to Men
    Link to this post

    I think I have a getcategories extension you can use as an example.

     

    https://gist.github.com/stevescotthome/4333272

  3. Men
    Men avatar
    8 posts
    Registered:
    28 Mar 2014
    29 Mar 2014 in reply to Steve
    Link to this post

    Hi Steve,

    Thank you for your time. I have tried something similar, but I was not able to bind it to a column. Do you perhaps have a working example where a category name of an item is binded to a column by it's uniqueName?

  4. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    29 Mar 2014
    Link to this post

    I think the easiest bet then is to make a proxy object with those taxa already resolved, then bind to that

  5. Men
    Men avatar
    8 posts
    Registered:
    28 Mar 2014
    29 Mar 2014 in reply to Steve
    Link to this post

    Steve thank you, but I need a working example for this RadGrid with content from a dynamic module (created with the module builder). I have tried different approaches..still no luck!

     So it would be appreciated if anyone has a working example of showing category name(s) in a RadGrid column, where a Sitefinity module / dynamic module is binded.

  6. Men
    Men avatar
    8 posts
    Registered:
    28 Mar 2014
    30 Mar 2014
    Link to this post

    Does anyone has a working example of showing category name(s) in a RadGrid column?

  7. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    31 Mar 2014
    Link to this post

    I told you, you have the two choices...

     1) Resolve them on ItemDataBound to each column
    This is where you put the module builder code to get your items and bind them
    http://www.telerik.com/help/aspnet-ajax/grid-advanced-data-binding.html
    ...then in the GridTemplateColumn put in an asp:repeater.  You can find the repeater in the ItemDataBound event then bind the resolved categories to it (get them using my extension)....or use a literal and loop and form the html yourself.

    2) Resolve BEFOREHAND and bind to a custom object in NeedsDataSource...

  8. Men
    Men avatar
    8 posts
    Registered:
    28 Mar 2014
    31 Mar 2014 in reply to Steve
    Link to this post

    Hi Steve,

    I have used your extension and tried something out. I think i'm close but am getting an error: You are trying to access item that no longer exists.

     The code is:

    <telerik:GridTemplateColumn DataField="Category" HeaderText="Country" SortExpression="Category" UniqueName="Category">
        <ItemTemplate>
            <asp:Repeater ID="rptCountry" runat="server">
                <ItemTemplate><%#DataBinder.Eval(Container.DataItem, "Category")%></ItemTemplate>
            </asp:Repeater>
        </ItemTemplate>
    </telerik:GridTemplateColumn>

    protected void rgEvents_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
     
            var providerName = String.Empty;
            DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName);
            dynamicModuleManager.Provider.SuppressSecurityChecks = true;
            Type SportType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Sports.Sport");
     
            DynamicContent dynaItem = dynamicModuleManager.GetDataItem(SportType, e.Item.GetType().GUID);
     
            Repeater countryRepeater = e.Item.FindControl("rptCountry") as Repeater;
            if (countryRepeater != null)
            {
                countryRepeater.DataSource = GetCategories.GetCategory(dynaItem);
                countryRepeater.DataBind();
            }
        }

    Thank you for your support. I cannot find what's going wrong, i'm sure you will..

  9. Pavel Benov
    Pavel Benov avatar
    341 posts
    Registered:
    14 Mar 2016
    02 Apr 2014
    Link to this post
    Hi guys,

    You could try Steve's idea using a proxy object to assign the event properties which you want to display. Here is a sample which you can use on a simple aspx page to test:

    Markup:
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="BindGrid.aspx.cs" Inherits="SitefinityWebApp.BindGrid" %>
    <%@ Register Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" TagPrefix="telerik" %>
    <!DOCTYPE html>
     
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:ScriptManager id="sm" runat="server"></asp:ScriptManager>
        <div>
        <telerik:RadGrid ID="grid" runat="server">
     
        </telerik:RadGrid>
        </div>
        </form>
    </body>
    </html>

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Telerik.Sitefinity.Modules.Events;
    using Telerik.Sitefinity.Model;
    using Telerik.OpenAccess;
    using Telerik.Sitefinity.Taxonomies;
    using Telerik.Sitefinity.Taxonomies.Model;
     
    namespace SitefinityWebApp
    {
        public partial class BindGrid : System.Web.UI.Page
        {
            
            protected void Page_Load(object sender, EventArgs e)
            {
                var manager = EventsManager.GetManager();
                var events = manager.GetEvents().Where(ev => ev.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live).ToList();
     
                var dataSource = new List<ProxyItem>();
                foreach (var item in events)
                {
                    var proxyItem = new ProxyItem();
                    proxyItem.title = item.Title;
                    var categories = item.GetValue<TrackedList<Guid>>("Category");
                    if (categories.Count > 0)
                    {
                        TaxonomyManager tManager = TaxonomyManager.GetManager();
                        foreach (var category in categories)
                        {
                            var catName = tManager.GetTaxon<HierarchicalTaxon>(category).Title;
                            proxyItem.category += catName + "/";
                        }
     
                        proxyItem.category = proxyItem.category.Remove(proxyItem.category.LastIndexOf('/'));
                    }
                    else
                    {
                        proxyItem.category = "No category";
                    }
     
                    dataSource.Add(proxyItem);
                }
     
                grid.DataSource = dataSource;
                grid.DataBind();
            }
        }
        public struct ProxyItem
        {
            public string title { get; set; }
            public string category { get; set; }
        }
    }

    I hope this helps.

    Regards,
    Pavel Benov
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  10. Men
    Men avatar
    8 posts
    Registered:
    28 Mar 2014
    05 Apr 2014 in reply to Pavel Benov
    Link to this post

    Hi Pavel,

    Thank you for the provided code. I have used it and succesfully extended it to my needs.

    Thank you both for the help!

10 posts, 0 answered