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

[Solved] Dynamically generate grid columns

3 Answers 213 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Richard Guo
Top achievements
Rank 1
Richard Guo asked on 20 Feb 2010, 02:55 AM
Instead of defining fixed grid columns directly on MVC views, I need to programmatically generate grid columns based on some criteria - for example, I need to show some columns to one group of users and show different set of columns to another group of users. Is there a way to do this? Thanks.

3 Answers, 1 is accepted

Sort by
0
Accepted
Alex Gyoshev
Telerik team
answered on 22 Feb 2010, 09:51 AM
Hello Richard,

The columns lambda is a code block - you can execute whatever code you need inside it.

.Columns(columns =>
{
    if (username == "alex") {
        columns.Bound(c => c.Name);
    } else {
        columns.Bound(c => c.BirthDate);
    }

    columns.Bound(c => c.Active);
    columns.Command(conf => {
        conf.Edit();
        conf.Delete();
    });
})


Regards,
Alex Gyoshev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Richard Guo
Top achievements
Rank 1
answered on 22 Feb 2010, 05:05 PM
Great! Thank you very much Alex.
0
Bo
Top achievements
Rank 1
answered on 24 Oct 2011, 06:17 AM
Cool..

But if my data source is dynamic, means I don't know how many columns will be loaded and what's the columns' name,  seems the auto generated columns feature does not work properly, or I may do something wrong.

Here is the view page, which is a partial view.

@using Telerik.Web.Mvc.UI
@model IEnumerable<dynamic>
@(
 Html.Telerik().Grid(Model)
             .Name("Grid")
             .Columns(c =>
                       c.AutoGenerate(true))
             .DataBinding(dataBinding => dataBinding.Ajax().Select("_CustomBinding", "VLBox"))
            .Pageable()
            .Sortable()
            .Groupable()
            .Filterable()
        )

But I got a screen as attached. 


Tags
General Discussions
Asked by
Richard Guo
Top achievements
Rank 1
Answers by
Alex Gyoshev
Telerik team
Richard Guo
Top achievements
Rank 1
Bo
Top achievements
Rank 1
Share this question
or