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

How to build a GridTemplateColumn in code behind

1 Answer 90 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Brandon
Top achievements
Rank 1
Brandon asked on 29 Oct 2014, 03:13 PM
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)
{
return;
}
//TASKS ENABLED
if (x.Member.Equals("TaskTo"))
{
x.Visible = TaskingUtilities.TaskingEnabled;
}

if (new List<string>
{
"TaskTo",
"TakeOwnership",
"ReturnButton",
"RejectTask",
"CompleteTask",
"Approve",
"ApproveTo",
"Deny",
"DenyTo"
}.Contains(x.Member))
{
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;
}
else
{
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.
































1 Answer, 1 is accepted

Sort by
0
Petur Subev
Telerik team
answered on 31 Oct 2014, 01:05 PM
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
Telerik
 

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.

 
Tags
Grid
Asked by
Brandon
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
Share this question
or