jetberay
jetberay

Reputation: 133

Concatenate fields in Grid using Kendo UI asp.net mvc

I'm new to ASP.NET MVC using Kendo-UI and trying to load data from entity to Grid. is there a way to concatenate two fields in a column?

@(Html.Kendo().Grid(Model)
.Name("Grid")
.Columns(columns =>
        {
            columns.Bound(p => p.employeeId).Title("ID");
            columns.Bound(p => p.firstname +" "+p.lastname).Title("Name");               
        })
        .Pageable()
        .Sortable()
        .Scrollable(scr=>scr.Height(430))
        .Filterable()
        .DataSource(datasource => datasource
        .Ajax()
        .PageSize(20)
        .ServerOperation(false)
)

I tried this solution but I get an error.

{"Bound columns require a field or property access expression."}

Upvotes: 1

Views: 5624

Answers (2)

saquib adil
saquib adil

Reputation: 156

One of the ways you can achieve what you are trying to do is by creating a Name property in your Model with just a getter which concatenates firstname and lastname, like this

public string Name { get { return string.Format("{0} {1}", firstname, lastname); } }

Then you can bind the Name to the grid, like this

columns.Bound(p => p.Name);

You should be good to go...

Upvotes: 1

Amal Dev
Amal Dev

Reputation: 1978

You can make use of the row template function to achieve it. Please find the snippet below and full demo can be seen here.

@(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.EmployeeViewModel>()
.Name("grid")
.HtmlAttributes(new { style = "width: 750px;height:430px;" })
.Columns(columns =>
{
    columns.Template(e => { }).ClientTemplate(" ").Width(140).Title("Picture");
    columns.Bound(e => e.Title).Width(400).Title("Details");
    columns.Bound(e=> e.EmployeeID).Title("ID");
})
.ClientRowTemplate(
    "<tr data-uid='#: uid #'>" +
        "<td class='photo'>" +
           "<img src='" + Url.Content("~/Content/web/Employees/") +"#:data.EmployeeID#.jpg' alt='#: data.EmployeeID #' />" +
        "</td>" +
        "<td class='details'>" +
            "<span class='title'>#: Title #</span>" +
            "<span class='description'>Name : #: FirstName# #: LastName#</span>" +
            "<span class='description'>Country : #: Country# </span>" +
        "</td>" +
        "<td class='employeeID'>" +
            "#: EmployeeID #" +
        "</td>" +
     "</tr>"      
)

.Scrollable()

)

Upvotes: 1

Related Questions