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