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
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.


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

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.

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
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.

