Dharam Narayan
Dharam Narayan

Reputation: 21

show dynamically selected columns in asp.net mvc grid at runtime

I have an asp.net MVC application where I am using telerik grid to show the data/records.I am using the Entity Model.

My requirement is that sometime I want to show only some of the columns specified at the runtime/may the user select. How do I bind View with only those columns as selected by the user . Initially view is binded with Model class with all columns .

Is there any other way other than telerik to show the customized columns as selected by the user then it will be also OK .

Upvotes: 2

Views: 1257

Answers (2)

Steve
Steve

Reputation: 11

<%= Html.Telerik() 
    .Grid(Model.Customers) 
    .Name("Grid") 
    .Columns(columns => 
    { 
        columns.Bound(customer => customer.FirstName).Visible(Model.IsShowFirstName); 
        columns.Bound(customer => customer.LastName).Visible(Model.IsShowLastName); 
    }) 
%>

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

You could customize the columns that are shown using the Columns method. You need to have the information about which columns need to be shown in the view model so that you can at runtime select the columns to show:

<%= Html.Telerik()
        .Grid(Model.Customers)
        .Name("Grid")
        .Columns(columns =>
        {
            if (Model.IsShowFirstName)
            {
                columns.Bound(customer => customer.FirstName);
            }
            if (Model.IsShowLastName)
            {
                columns.Bound(customer => customer.LastName);
            }
        })
%>

Upvotes: 1

Related Questions