I'm trying to display an IEnumerable<string> in a grid. As far as I can tell, this is impossible to achieve with sorting and filtering. Is that correct?
When I try to bind the grid I get the error: Bound columns require a field or property access expression
<% Html.Telerik().Grid(Model.Roles) .Name("grid") .Columns(col => { col.Bound(role => role.ToString()).Template(role => { %> <%= Html.ActionLink(role, "details", new { id = role }) %> <% }).Title("Role Name"); .Pageable() .Sortable() .Filterable() .Render();%>The easy way to fix it would be to use a simple Template, but in order to get paging and sorting, I must have the Bound() part.
Please help!
4 Answers, 1 is accepted
Indeed we don't currently support this scenario. I have logged it for further investigation. If possible we would fix it for the next official release.
Kind regards,
the Telerik team
The .Add() method in the Grid control is no longer used, instead of Add use Bound(), Template() or a combo of both:
i. Bound() example (use if Filterable or Format is required for the column, as Template does not allow those properties to be set):
//OLD WAY, USING Add():
columns.Add(o => o.CreatedDate).Format("{0:MM/dd/yyyy}").Width(40).Title("Date");
//NEW WAY, USING Template():
columns.Bound(o => o.CreatedDate).Format("{0:MM/dd/yyyy}").Width(40).Title("Date");
ii. Template() example (can use if straight forward column declaration is needed, with no additional properties such as Filterable or Format):
//OLD WAY, USING Add():
columns.Add(p => Html.ActionLink("Impersonate", "SetUser", new {emplId= p.EMPLID})).Width(100);
//NEW WAY, USING Template():
columns.Template(emp => Html.ActionLink("Impersonate", "SetUser", new {emplId = emp.EMPLID})).Width(100);
iii. Both methods example (use when Bound is necessary in order to set Filterable or Format, but when an Html helper method is also needed, such as ActionLink):
//OLD WAY, USING Add():
columns.Add(o => Html.ActionLink("View", "Details", "Award", new { id = o.AwardId }, ""))
.Width(10).Title("Action").Filterable(false);
//NEW WAY, USING Bound() AND Template():
columns.Bound(o => o.AwardId)
.Template(o => Html.ActionLink("View", "Details", "Award", new { id = o.AwardId }, ""))
.Width(10).Title("Action").Filterable(false);
Here's my grid's columns definitions:
.Columns(columns => {
// this column (bool) displays, and is both filterable and sortable. columns.Bound(rf => rf.locked).Title("Locked").Width(23)
.Template( item => Html.JQGridActionIconButton( ... snip ... )
);
// this column (string) displays, but is not sortable or filterable
columns.Bound(rf => rf.filename).Template(@<text> @(Html.ActionLink(item.filename, "ViewFile", null, new { id = item.fullpathNoPrefix }, null)) @(Html.ActionLink("(download)", "DownloadFile", null, new { id = item.fullpathNoPrefix }, null)) </text>).Title("File Name").Width(200); columns.Bound(rf => rf.description).Width(200).Title("Description");
// this column (int) doesn't work (the column doesn't appear in the grid at all) columns.Bound(rf => rf.size).Template(rf => string.Format("{0:###,###}", rf.size)).Width(50).Title("Size");
// this line works, but column isn't sortable
columns.Template(rf => string.Format("{0:###,###}", rf.size)).Width(50).Title("Size");
// this column (string) displays just fine columns.Bound(rf => rf.mime_type.description).Width(200).Title("File Type (mime-type)");
// this column (DateTime) doesn't work (column doesn't appear in grid) columns.Bound(rf => rf.create_date).Template(rf => rf.create_date.ToString()).Width(100).Title("Modify Date");
// ..nor does this one (nullable DateTime) columns.Bound(rf=>rf.expires).Template(rf => rf.expires ? rf.expire_date.ToString() : "Never" ).Width(100).Title("Expire Date");
// If I remove the .Bound(...) in the expressions above and just use the Template(), the columns
// appear, but are not filterable/sortable columns.Command( rf => rf.Delete().ButtonType(GridButtonType.Image)).Width(40); })Any ideas? The bound object, is an EF1 generated class.
-- pryankster
I have a column bound like this:
c.Bound(o => o.StatusId).Template(o => o.Status.Name);
There are various statuses in a table in the db, like "Active," "Inactive," etc. But I need to bind to the key in that table, which is the StatusId. I have edit and display templates that deal with displaying a dropdown box to choose the status on editing, and the template deals with displaying the status name to the user, not the StatusId.
The problem is with filtering. The filters are being applied to the StatusId, which is useless to the user. They need to be applied to the Status.Name in the template.
How do I bind to the StatusId (int), but filter on the Name (string)?