MVC 3 Webgrid - how do you hide columns you do not want to be visible?
Asked Answered
Y

3

13

I have a webgrid and there is a column I want to be visible only to certain users. Currently I have coded the grid as follows

if (Context.User.IsInRole(Role.Inputter) || Context.User.IsInRole(Role.Administrator))
{
    @grid.GetHtml(columns: grid.Columns(
        grid.Column(format: (item) => Html.ActionLink("Select", "Details", new { contractId = item.ContractId })),
        grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { contractId = item.ContractId })),
        grid.Column("SignOffDate", "Sign Off Date",
            format: @<text> <span>@item.SignOffDate.ToString("d/M/yyyy")</span></text>),
        grid.Column("FullContractNumber", "Contract Number"),
        grid.Column("ContractTitle", "Title")
    ));
}
else
{ 
    @grid.GetHtml(columns: grid.Columns(
        grid.Column(format: (item) => Html.ActionLink("Select", "Details", new { contractId = item.ContractId })),
        grid.Column("SignOffDate", "Sign Off Date",
            format: @<text> <span>@item.SignOffDate.ToString("d/M/yyyy")</span></text>),
        grid.Column("FullContractNumber", "Contract Number"),
        grid.Column("ContractTitle", "Title")
    ));
}

But surely there is a better way without repeating all that code? The only difference between the 2 column inputs is that I want to display the Edit link for particlaur users. So what is the best alternative way of doing that?

Yseult answered 24/8, 2011 at 8:15 Comment(0)
S
27

Try like this (untested, don't have access to VS at the moment):

@{
    var gridColumns = new List<WebGridColumn>();
    gridColumns.Add(grid.Column(format: (item) => Html.ActionLink("Select", "Details", new { contractId = item.ContractId })));
    if (Context.User.IsInRole(Role.Inputter) || Context.User.IsInRole(Role.Administrator))
    {
        gridColumns.Add(grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { contractId = item.ContractId })));
    }
    gridColumns.Add(grid.Column("SignOffDate", "Sign Off Date", format: @<text> <span>@item.SignOffDate.ToString("d/M/yyyy")</span></text>));
    gridColumns.Add(grid.Column("FullContractNumber", "Contract Number"));
    gridColumns.Add(grid.Column("ContractTitle", "Title"));
}

@grid.GetHtml(columns: grid.Columns(gridColumns.ToArray()));
Scaife answered 24/8, 2011 at 17:26 Comment(1)
WOW... great solution Darin. Working as expected.Gott
T
0
grid.Column("FriendlyId", style:"hidecol",header:"")

Instead of using it like this you should use it like in the manner bellow. I've tried, it will work successfully.

grid.Column(format: @<input type="hidden" name="FriendlyId" value="@item.FriendlyId" />)
Thomasthomasa answered 8/1, 2014 at 9:2 Comment(0)
O
0

Not sure if it can me made more simpler like this by using "columnNames" parameter. I wanted to show "CustomerCode" column so have just put "CustomerCode" any other column gets excluded.

WebGrid obj = new WebGrid(Custs,columnNames: new[] { "CustomerCode"});

Taken from

http://www.codeproject.com/Articles/843788/WebGrid-in-ASP-NET-MVC-important-tips#Tip3:-DisplayNecessaryColumnsMVCWebGrid

Otalgia answered 20/11, 2014 at 13:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.