How to build a GridTemplateColumn in code behind

2 posts, 0 answers
  1. Brandon
    Brandon avatar
    5 posts
    Member since:
    Mar 2014

    Posted 29 Oct 2014 Link to this post

    I need to know how to properly build a gridtemplatecolumn in code behind.

    @(Html.Kendo().Grid<DealViewModel>().Name("Grid").AutoBind(false).Scrollable(x => x.Height("auto")).Columns(columns =>
                                                                                                                  columns.AutoGenerate(a =>
                                                                                                                      a.Encoded = false;
                                                                                                                  })).Columns((columns) => Utilities.SetColumnProperties(columns.Container.Columns, @ViewBag.CurrentStepId)).Sortable(x=>x.SortMode(GridSortMode.SingleColumn))

    the second columns call the SetColumnProperties method where I query the database to get the columns properties that are set by the user as follows.


    public static void SetColumnProperties(IList<GridColumnBase<DealViewModel>> columns, Guid currentStepId)
    var column = ContextHelper.Context.SalesStep.Find(currentStepId).Column.ToList();
    var columnsToAdd = new List<GridColumnBase<DealViewModel>>();

    columns.ForEach(x =>
    var currentColumn = column.FirstOrDefault(y => y.ColumnName.Name.Equals(x.Member, StringComparison.InvariantCultureIgnoreCase));

    x.Visible = currentColumn != null;
    var gridBoundColumn = x as IGridBoundColumn;
    if (gridBoundColumn == null || currentColumn == null)
    if (x.Member.Equals("TaskTo"))
    x.Visible = TaskingUtilities.TaskingEnabled;

    if (new List<string>
    var claims = ClaimsPrincipal.Current.Claims;
    var firstOrDefault = claims.FirstOrDefault(y => y.ValueType.Equals(currentStepId.ToString()));
    if (firstOrDefault != null)
    var enumString = firstOrDefault.Value;
    SedonaSalesWorkflow.StepAccessLevel returnVal;
    var stepAccessLevel = Enum.TryParse(enumString, true, out returnVal) ? returnVal : SedonaSalesWorkflow.StepAccessLevel.None;

    x.Visible = stepAccessLevel == SedonaSalesWorkflow.StepAccessLevel.Full;
    x.Visible = false;

    if (currentColumn.Width != 0)

    x.Width = currentColumn.Width.ToString(CultureInfo.InvariantCulture) + "px";
    gridBoundColumn.Hidden = currentColumn.IsHidden;
    gridBoundColumn.Title = currentColumn.Title;
    gridBoundColumn.Format = currentColumn.Format;
    gridBoundColumn.Filterable = currentColumn.Filterable;
    gridBoundColumn.Sortable = currentColumn.Sortable;
    gridBoundColumn.Groupable = currentColumn.Groupable;                


    Now how do I add a GridTemplateColumn correctly.  I can get it to show but I want to build a column dynamically from code behind.  There is basically one grid that users can add/remove fields to and create a new page and then set properties on that field which is why this is done this way.  I would like to basically concatenate columns of a certain type.

    This currently does not work
    columns.FirstOrDefault().Grid.Columns.Add(new GridTemplateColumn<DealViewModel>(columns.FirstOrDefault().Grid, model => model.Approve)
                    ClientTemplate = "#= Approve #"

    Thanks for the help.

  2. Petur Subev
    Petur Subev avatar
    1882 posts

    Posted 31 Oct 2014 Link to this post

    Hello Brandon,

    Defining the template within the controller is not possible because the @<text></text> require  access to the ViewContext and more specifically the Write() method of the Writer which is not accessible within the controller. You can however define ClientTemplate which is just a simple string. 

    Kind Regards,
    Petur Subev

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

Back to Top