开发者

How to hide a column from a Telerik MVC grid and ALSO from the Edit view

开发者 https://www.devze.com 2023-03-08 11:46 出处:网络
I have a Telerik MVC grid. 1 of the columns needs to be hidden from certain users. This is easy to achieve using .Visible

I have a Telerik MVC grid.

1 of the columns needs to be hidden from certain users.

This is easy to achieve using .Visible

The problem is, when I launch the Edit mode for the row, the co开发者_JAVA技巧lumn I want hidden is displayed in the edit view.

Any idea how to hide the column from both the grid view and the edit view?

Edit: I'm using Pop Up edit mode and Ajax binding.


Hiding a column from the edit form depends on the edit mode. You have not specified which edit mode you are using so I will give a solution for every single one:

  • Inline and InCell - you just need to make the column Readonly() and it will no longer be editable.

  • Popup or InForm - those modes use Html.EditorForModel which displays all properties of the model by design. As a result even the properties which are not bound as columns will be shown. Hiding something from the edit form must be done with JavaScript via the OnEdit event. However the OnEdit event is raised only during Ajax binding. It is not clear from your description whether you are using ajax or server binding. If you use server binding you have to hook to the OnLoad JavaScript event and hide the unnecessary column from the editor using jQuery.


In addition to Atanas' excellent response, if you're using Server binding, you can use the fact that Html.EditorForModel uses ShowForDisplay and ShowForEdit properties to determine when to create scaffolds for the properties. Unfortunately, there's no built-in way to set those independently, but you can create and register your own ModelMetadataProvider. I used the code from this SO answer, and it let me easily specify what gets displayed on edit forms in C# code.

Showing Different fields in EditorForModel vs. DisplayForModel modes in MVC2


I hope this helps... and sorry for all the space between lines but without it, the code looked messy.

Like Atanas Korchev said, you can use javascript but there is a much simpler way. you can define your column as <ScaffoldColumn(False)> :

Public Class EntityS

    <Key()> _
    <ComponentModel.ReadOnly(True)> _
    <DisplayName("Id")> _
    <ComponentModel.DataAnnotations.Editable(False)> _
    **<ScaffoldColumn(False)> _**
    Public Property Id_EntityS As Integer

    ...

    ...

End Class



@(Html.Telerik().Grid(Of EntityS)() _

   .Name("Grid") _

   .DataKeys(Sub(keys)

             keys.Add("Id_EntityS")

    End Sub) _

    .ToolBar(Sub(commands)

                 commands.Insert().ButtonType(GridButtonType.ImageAndText) _
                                 .ImageHtmlAttributes(New With {.style = "margin-left:0"})

                 End Sub) _

      .DataBinding(Sub(dataBinding)

       dataBinding.Ajax() _

       ...

       End Sub) _

       .Columns(Sub(columns)

        ...

        columns.Command(Sub(commands)

        commands.Edit().ButtonType(GridButtonType.ImageAndText)

        commands.Delete().ButtonType(GridButtonType.ImageAndText)

        commands.Edit.HtmlAttributes("onedit")

        End Sub).Width(180).Title("Commands")

        End Sub) _

        .Editable(Sub(editing)

        editing.Mode(GridEditMode.PopUp).Window(Sub(window)

        End Sub)

        End Sub) _

        ....)
0

精彩评论

暂无评论...
验证码 换一张
取 消