Create a telerik MVC grid in codebehind?

advertisements

I've been trying to figure out how to create a telerik MVC grid in codebehind? The telerik docs have a way of doing it with RadGrid but not their MVC Grid control..

What I'm basically trying to do is create a grid based on some conditions, like I may have a grid like this:

 Html.Telerik().Grid<ZeDate>("dates")
        .Name("MyGrid")
        .Pageable(paging => paging.PageSize(10))
        .Sortable()
        .Filterable()
        .Groupable()
        .ColumnContextMenu()
        .DataKeys(keys => keys.Add(c => c.id))
        .DataBinding(dataBinding =>
        {
            dataBinding.Ajax()
            .Select("_SelectAjaxEditing", "Dates")
            .Insert("_InsertAjaxEditing", "Dates")
            .Update("_SaveAjaxEditing", "Dates")
            .Delete("_DeleteAjaxEditing", "Dates");
        })
        .ToolBar(commands => commands.Insert())
        .Columns(columns =>
        {
            columns.Bound(o => o.name);
            columns.Bound(o => o.date1);
            columns.Command(commands =>
            {
                commands.Edit().ButtonType(GridButtonType.Text);
                commands.Delete().ButtonType(GridButtonType.BareImage);
            }).Width(185);
        })
        .Editable(editing => editing.Mode(GridEditMode.InForm))
        .Render();

But I might in some cases want to not show a specific column such as 'date1' or I might not want to allow the grid to be filterable, basically stuff like that.


Besides the "no code-behind" that Mystere Man mentioned, there are several different ways to do conditional aspects in an MVC view with Telerik. Since Telerik uses a fluent interface you can set it to a variable and take advantage of that. So for example you could do:

var telerik = Html.Telerik().Grid<ZeDate>("dates")
    .Name("MyGrid")
    .Columns(columns =>
    {
        columns.Bound(o => o.name);

        // Only render the date column if the designated
        if (Model.CanSeeDate)
        {
            columns.Bound(o => o.date1);
        }
    });

// Only let the grid be filterable if allowed
if (Model.GridFilterable)
{
    telerik = telerik.Filterable();
}

// Perform other telerik setup
telerik.Render();