Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / Telerik MVC Extensions (superseded) > General Discussions > Conditional bound column Grid

Not answered Conditional bound column Grid

Feed from this thread
  • Jose avatar

    Posted on Jul 1, 2012 (permalink)

    Hi, im kind of new using Telerik Grid, im trying to "emulate" the folowing code  

    if (item.Prop!= DateTime.MinValue){
    Html.DisplayFor(modelItem => item.Prop);
    }

    Basicaly is depending on the value of the column show it or not to the user. how can i do that using the Grid ??

    Meabi something like 

    columns.Bound(if(item.prop = DateTime.MinValue) o=>o.prop)

    Reply

  • Pedro avatar

    Posted on Jul 10, 2012 (permalink)

    You achieve this by doing like so:

    @( Html.Telerik().Grid(Model)
        .Name("Grid")
        .ToolBar(commands => commands.Insert())
        .Columns(columns =>
        {
      
            if (item.Prop == DateTime.MinValue)
            {
                columns.Bound(o => o.prop);
            }
     
        ...
        }
    ...
    )


    Pedro

    Reply

  • Jose avatar

    Posted on Jul 11, 2012 (permalink)

    Thanks for the reply, but apparently the item element its not available at that point:

    Compiler Error Message: CS0103: The name 'item' does not exist in the current context 

    here's my code

        Html.Telerik().Grid((IEnumerable<Movies>)ViewBag.MoviesSet)
            .Name("grid")
            .DataKeys(dk => dk.Add(k => k.Id))
            .Columns(columns =>
                         {
                             if (item.State == "x")
                             {
                                 columns.Bound(pp => pp.State);
                                
                             }
                             columns.Bound(pp => pp.Id).Hidden(true);
                             columns.Bound(pp => pp.Name);
                         })
            .Pageable()
            .Sortable()
            .Selectable()
            .Groupable()
            .ClientEvents(events => events.OnRowSelect("onRowSelected"))
            .Render();

    Reply

  • Pedro avatar

    Posted on Jul 12, 2012 (permalink)

    I see now what you're trying to do, but i dont understand why. But in the spirit of answering your question, this is how I think you'd answer it.

    .Columns(columns =>
               {
                     columns.Bound(pp => pp.State);
               })
    .CellAction(cell =>
            {
                if (cell.Column.Title == "State")
                {
                    if (cell.DataItem.State != null)
                    {
                        if (cell.DataItem.State != "x")
                        {
                            cell.HtmlAttributes["style"] = "display:none";
                        }
                    }
                }
            })

    Reply

  • Jose avatar

    Posted on Jul 12, 2012 (permalink)

    what i was trying to accomplish was showing some data depending on a condition:

    Id Name State
    1 Terminator Active
    2 Terminator 2  
    3 Terminator 3 Inactive

    I think the best way it to sue the hidden() method, am i right ??

    Reply

  • Pedro avatar

    Posted on Jul 12, 2012 (permalink)

    Using the .CellAction didn't work?

    If it broke your grid, try using "visibility:hidden" instead of "display:none"

    Pedro

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / Telerik MVC Extensions (superseded) > General Discussions > Conditional bound column Grid