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

Conditional bound column Grid

5 Answers 88 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.
Jose
Top achievements
Rank 1
Jose asked on 02 Jul 2012, 04:16 AM
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)

5 Answers, 1 is accepted

Sort by
0
Pedro
Top achievements
Rank 2
answered on 10 Jul 2012, 07:32 PM
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
0
Jose
Top achievements
Rank 1
answered on 12 Jul 2012, 04:30 AM
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();
0
Pedro
Top achievements
Rank 2
answered on 12 Jul 2012, 12:07 PM
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";
                    }
                }
            }
        })
0
Jose
Top achievements
Rank 1
answered on 12 Jul 2012, 03:25 PM
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 ??
0
Pedro
Top achievements
Rank 2
answered on 12 Jul 2012, 05:08 PM
Using the .CellAction didn't work?

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

Pedro
Tags
General Discussions
Asked by
Jose
Top achievements
Rank 1
Answers by
Pedro
Top achievements
Rank 2
Jose
Top achievements
Rank 1
Share this question
or