RadGrid - Conditional cell display in serverside

Thread is closed for posting
10 posts, 0 answers
  1. Sub
    Sub avatar
    91 posts
    Member since:
    Nov 2007

    Posted 08 May 2009 Link to this post

    In a MVC view page, I set the data source of the Grid control.
     
    I want to conditionally populate the cells based on the data in the server side of the view in C#. How do I populate it?
     
    I used to do it in the ItemDataBound server side event. Is there any way I can loop throgh the datasource and declarativley define the ItemTemplate of the GridTemplateColumn? Please let me know how I can do conditional display in server side.

    Thanks

  2. Rosen
    Admin
    Rosen avatar
    3234 posts

    Posted 11 May 2009 Link to this post

    Hi,

    In order to achieve the desired functionality you may use a small helper method which to perform the transformation. Similar to the following:

    Column declaration:
              <telerik:GridTemplateColumn> 
                    <HeaderTemplate> 
                       EmployeeID  
                    </HeaderTemplate> 
                    <ItemTemplate> 
                        <%#FormatValue(((GridDataItem) Container)) %>                      
                    </ItemTemplate>                  
                </telerik:GridTemplateColumn> 

    Code behind:
            protected string FormatValue(GridDataItem dataItem)  
            {  
                var fieldValue = (int)DataBinder.Eval(dataItem.DataItem, "MyFieldName");  
     
                if(fieldValue > 20)  
                {  
                    return "some text";  
                }  
                return "some other text";  
            } 


    Sincerely yours,
    Rosen
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Sub
    Sub avatar
    91 posts
    Member since:
    Nov 2007

    Posted 11 May 2009 Link to this post

    When you said "Code behind" did you mean "View"? Writing a method in a View?

  5. Sub
    Sub avatar
    91 posts
    Member since:
    Nov 2007

    Posted 12 May 2009 Link to this post

    I think Html.RenderPartial is a better way if we have multiple controls to be rendered on conditional basis on the Model object. I tried to pass the GridDataItem in various possible syntax with no success. I want to have something like below and what is the correct syntax to make it work?

     

    <ItemTemplate>

     

     

    <%

     

    Html.RenderPartial("ConditionalCell", Eval("TestCol")); %>

     

     

     

    </ItemTemplate>

     

     

  6. Sub
    Sub avatar
    91 posts
    Member since:
    Nov 2007

    Posted 13 May 2009 Link to this post

    Is there any update from Telerik on how to use RenderPartial inside the item template?
  7. Rosen
    Admin
    Rosen avatar
    3234 posts

    Posted 13 May 2009 Link to this post

    Hello,

    You may implement your own extension methods which to build the appropriate markup.

    Here is a simple example:

    using System.Web.Mvc;  
    using System.Web.Mvc.Html;  
    using System.Web.UI;  
    using Telerik.Web.UI;  
     
    namespace MyNamespace  
    {  
        public static class MyHtmlExtensions  
        {  
            public static string RenderCellContent<T>(this HtmlHelper helper, T dataItem) where T: GridItem  
            {  
                var fieldValue = (int)DataBinder.Eval(dataItem.DataItem, "EmployeeID");  
     
                if (fieldValue > 20)  
                {  
                    return helper.ActionLink("some link text""some actionName");  
                }  
                return helper.TextBox("some textbox""some value");  
            }  
        }  

    and a simple usage:

               <telerik:GridTemplateColumn> 
                    <HeaderTemplate> 
                       EmployeeID  
                    </HeaderTemplate> 
                    <ItemTemplate>                                    
                        <%#Html.RenderCellContent(Container)%> 
                    </ItemTemplate>                  
                </telerik:GridTemplateColumn> 

    I hope this helps and will get you started in building your own extension methods.

    Kind regards,
    Rosen
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  8. FRANK
    FRANK avatar
    5 posts
    Member since:
    Feb 2009

    Posted 07 Aug 2009 Link to this post

    Telerik Team,

    In Radgrid, I would like to provide hyperlink in a column based on data condition.

    Example: Let say I have column Named "ApprovalStatus" which will have values like "To Be Approved", "Approved" or  "Rejected". I need to provide link when cell value is "To Be Approved" else no url link. On click of "To Be Approved", will redirect new page.

    I don't have System.Web.MVC library installed. I am using Telerik components version 2008.3.1314.35.
    I would like to know what is the best to achieve this in telerik radgrid without using MVC? Any help will be highly appreciated.

    Thanks,
    Ram
    BBVA 

         
  9. Yavor
    Admin
    Yavor avatar
    11 posts

    Posted 12 Aug 2009 Link to this post

    Hi Andres,

    Basically, you can provide the hyperlink by default, in either a gridHyperlinkColumn, or a template column. Then, you can simply hide the hyperlink conditionally. This may be similar to the approach demonstrated in the following article:

    http://www.telerik.com/help/aspnet-ajax/grdconditionalimagedisplayingridcolumn.html

    I hope this information helps.

    Best wishes,
    Yavor
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  10. Jon Wood
    Jon Wood avatar
    3 posts
    Member since:
    Aug 2009

    Posted 19 May 2011 Link to this post

    I'm looking for a similar solution and the link provided is broken - is there an updated link for this? Does this content still exist?  Thx!
  11. Jon Wood
    Jon Wood avatar
    3 posts
    Member since:
    Aug 2009
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017