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

RadGrid - Conditional cell display in serverside

9 Answers 290 Views
UI for ASP.NET AJAX in ASP.NET MVC
This is a migrated thread and some comments may be shown as answers.
Sub
Top achievements
Rank 1
Sub asked on 08 May 2009, 08:19 PM

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

9 Answers, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 11 May 2009, 03:01 PM
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.
0
Sub
Top achievements
Rank 1
answered on 11 May 2009, 03:20 PM
When you said "Code behind" did you mean "View"? Writing a method in a View?

0
Sub
Top achievements
Rank 1
answered on 12 May 2009, 07:35 PM
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>

 

 

0
Sub
Top achievements
Rank 1
answered on 13 May 2009, 03:26 PM
Is there any update from Telerik on how to use RenderPartial inside the item template?
0
Rosen
Telerik team
answered on 13 May 2009, 03:52 PM
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.
0
FRANK
Top achievements
Rank 1
answered on 07 Aug 2009, 05:15 PM

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 

     
0
Yavor
Telerik team
answered on 12 Aug 2009, 07:52 AM
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.
0
Jon Wood
Top achievements
Rank 1
answered on 19 May 2011, 08:02 PM
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!
0
Jon Wood
Top achievements
Rank 1
answered on 19 May 2011, 08:15 PM
Tags
UI for ASP.NET AJAX in ASP.NET MVC
Asked by
Sub
Top achievements
Rank 1
Answers by
Rosen
Telerik team
Sub
Top achievements
Rank 1
FRANK
Top achievements
Rank 1
Yavor
Telerik team
Jon Wood
Top achievements
Rank 1
Share this question
or