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

Forums / Developing with Sitefinity / Custom Module Displayed in RadGrid with Complex Databinding

Custom Module Displayed in RadGrid with Complex Databinding

2 posts, 0 answered
  1. Matt
    Matt avatar
    35 posts
    Registered:
    01 Nov 2010
    01 Aug 2012
    Link to this post
    I created a custom module and was able to use the sample code to display most of the information in a RadGrid but what I can't figure out is how to display items that are not the standard short text value, such as image, Yes/No or dropdowns.  I assume I need to use a helper class but I'm not sure how to do it in a RadGrid.

    I found this helper class in the showcases demo but I don't know how to make it work with a Grid.
    protected string GetImageUrl(object item)
            {
                var retval = string.Empty;
                var showcase = item as DynamicContent;
                if (showcase == null) return retval;
     
                var contentLinks = (ContentLink[])showcase.GetValue("Screenshot");
                ContentLink imageContentLink = contentLinks.FirstOrDefault();
                LibrariesManager libraryManager = LibrariesManager.GetManager();
                var image = libraryManager.GetImage(imageContentLink.ChildItemId);
                if (image == null) return retval;
     
                retval = image.Url;
                return retval;
            }
    Here's my current Grid.
    <telerik:RadGrid ID="Grid" AutoGenerateColumns="False" runat="server" PageSize="4"
        Skin="Black" ShowStatusBar="True" ShowFooter="True" AllowPaging="True" CellSpacing="0"
        GridLines="None" Width="646px">
        <ClientSettings>
            <Selecting AllowRowSelect="True" />
        </ClientSettings>
        <MasterTableView>
            <RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
            </RowIndicatorColumn>
            <ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
            </ExpandCollapseColumn>
            <Columns>
                <telerik:GridBoundColumn HeaderText="Company" DataField="Company" />
                <telerik:GridBoundColumn HeaderText="First Name" DataField="FirstName" />
                <telerik:GridBoundColumn HeaderText="Last Name" DataField="LastName" />
                <telerik:GridCheckBoxColumn HeaderText="Active" DataField="Active" />
                <telerik:GridBoundColumn HeaderText="D.O.B." DataField="Dob" DataFormatString="{0:d}" />
                <telerik:GridBoundColumn HeaderText="Membership" DataField="Membership" />
                <telerik:GridBoundColumn HeaderText="Screenshot" DataField="Screenshot" />
            </Columns>
            <EditFormSettings>
                <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                </EditColumn>
            </EditFormSettings>
        </MasterTableView>
        <FilterMenu EnableImageSprites="False">
        </FilterMenu>
    </telerik:RadGrid>
     Here's the code behind
    using System;
    using System.Linq;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Telerik.Sitefinity.DynamicModules;
    using Telerik.Sitefinity.Data.Linq.Dynamic;
    using Telerik.Sitefinity.DynamicModules.Model;
    using Telerik.Sitefinity.GenericContent.Model;
    using Telerik.Sitefinity.Modules.Libraries;
    using Telerik.Sitefinity.Model;
    using Telerik.Sitefinity.Model.ContentLinks;
    using Telerik.Sitefinity.Utilities.TypeConverters;
     
     
    namespace SitefinityWebApp.Modules.Contacts
    {
        public partial class DisplayItems : System.Web.UI.UserControl
        {
            protected void Page_Load(object sender, EventArgs e)       
            {
                var myCollection = GetDataItems();
                Grid.DataSource = myCollection;
                Grid.DataBind();
                
            }
     
     
            public IQueryable<DynamicContent> GetDataItems()        
            {            
                DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();            
                Type contactType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Contacts.Contact");              
                var myCollection = dynamicModuleManager.GetDataItems(contactType).Where("Active == true").Where(i => i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live && i.Visible == true);
                    
                return myCollection;        
            }
     
            
     
                     
        }
    }
  2. Jonathan
    Jonathan avatar
    1 posts
    Registered:
    15 Dec 2012
    15 May 2013 in reply to Matt
    Link to this post
    Hi Matt,

    I found my self in a similar situation as you.  Here is what I came up with.

    The "Logo" column holds the Title of the image and then the Image column holds the actual image. Now for check boxes, etc. makes sure to create the correct column type and you should be able to follow the same ideas here.  Here is some RadGrid documents from Telerik.  I placed an export button on my to export to csv, if needed.

    ASPX Page

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ExportSchools.aspx.cs" Inherits="SitefinityWebApp.Custom.ExportSchools" %>
     
    <!DOCTYPE html>
     
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:ScriptManager runat="server" ID="scriptMana"></asp:ScriptManager>
        <div>
            <telerik:RadGrid runat="server" ID="grid" AutoGenerateColumns="false" OnItemDataBound="grid_ItemDataBound">
                <MasterTableView>
                    <Columns>
                        <telerik:GridBoundColumn DataField="Title" DataType="System.String" HeaderText="Title" UniqueName="Title">
                        </telerik:GridBoundColumn>
                        <telerik:GridHTMLEditorColumn DataField="Description" HeaderText="Description" UniqueName="Description"></telerik:GridHTMLEditorColumn>
                        <telerik:GridHTMLEditorColumn DataField="Summary"  HeaderText="Summary" UniqueName="Summary"></telerik:GridHTMLEditorColumn>
                        <telerik:GridHTMLEditorColumn DataField="Programs" HeaderText="Programs" UniqueName="Programs"></telerik:GridHTMLEditorColumn>
                        <telerik:GridBoundColumn DataField="Logo" HeaderText="Logo" UniqueName="Logo"></telerik:GridBoundColumn>
                        <telerik:GridImageColumn HeaderText="Logo" UniqueName="Image" ></telerik:GridImageColumn>
                    </Columns>
                </MasterTableView>
                <ExportSettings OpenInNewWindow="true" FileName="Data" IgnorePaging="true">
                    <Csv ColumnDelimiter="Comma" RowDelimiter="NewLine" FileExtension="txt" />
                </ExportSettings>
            </telerik:RadGrid>
            <asp:Button runat="server" ID="button" OnClick="button_Click" Text="Export" />
        </div>
        </form>
    </body>
    </html>

    Code Behind

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Telerik.Sitefinity.DynamicModules;
    using Telerik.Sitefinity.DynamicModules.Model;
    using Telerik.Sitefinity.Utilities.TypeConverters;
    using Telerik.Sitefinity.GenericContent.Model;
    using Telerik.Sitefinity.Model;
    using Telerik.Sitefinity.Model.ContentLinks;
    using Telerik.Sitefinity.Modules.Libraries;
    using Telerik.Web.UI;
    using System.Data;
     
    namespace SitefinityWebApp.Custom
    {
        public partial class ExportSchools : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                DynamicModuleManager dynMan = DynamicModuleManager.GetManager();
                Type certificationSchoolType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Certificationschools.CertificationSchool");
     
                var items = dynMan.GetDataItems(certificationSchoolType).Where(t => t.Status == ContentLifecycleStatus.Live).ToList<DynamicContent>();
     
                grid.DataSource = items;
                grid.DataBind();
            }
     
            protected Telerik.Sitefinity.Libraries.Model.Image GetImageUrl(object item)
            {
                Telerik.Sitefinity.Libraries.Model.Image retval = null;
                var showcase = item as DynamicContent;
                if (showcase == null) return retval = null;
     
                var contentLinks = (ContentLink[])showcase.GetValue("Logo");
                ContentLink imageContentLink = contentLinks.FirstOrDefault();
                LibrariesManager libraryManager = LibrariesManager.GetManager();
                if (imageContentLink != null)
                {
                    var image = libraryManager.GetImage(imageContentLink.ChildItemId);
                    if (image == null) return retval = null;
     
                    retval = image;
                }
                return retval;
            }
     
            protected void button_Click(object sender, EventArgs e)
            {
                grid.MasterTableView.ExportToCSV();
            }
     
            protected void grid_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
            {
                TableCell cell = null;
                TableCell cell2 = null;
                GridDataItem item = null;
                if (e.Item is GridDataItem)
                {
                    item = (GridDataItem)e.Item;
                    cell = (TableCell)item["Logo"];
                    cell2 = (TableCell)item["Image"];
                }
     
                if (e.Item.DataItem is DynamicContent && cell != null)
                {
                    var img = GetImageUrl(e.Item.DataItem);
                    cell.Text = (img != null) ? img.Title.ToString() : "No Logo";
     
                    if (item["Image"].Text == " ")
                    {
                        Image image = (Image)item["Image"].Controls[0];
                        image.ImageUrl = (img != null) ? img.Url : "No Logo";
                    }
                }
                
            }
        }
    }

2 posts, 0 answered