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

Column count for related table

1 Answer 283 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dario
Top achievements
Rank 1
Veteran
Dario asked on 26 Oct 2020, 03:28 PM

Hi to all,

I have this collection routine

contacts = from rec in _context.Contacts.Include("SellToContacts")
                           where rec.ParentContactFK.HasValue == false
                           select rec;
DataSourceResult result = contacts.ToDataSourceResult(request);
 
return Json(result);

Contact model is configured in this mode

[...]
public Contact ContactParent { get; set; }
         
[Column("Parent Contact Id", TypeName = "int")]
[Display(Name = "Società Principale")]
public int? ParentContactFK { get; set; }
 
[ForeignKey("ParentContactFK")]
public ICollection<Contact> SellToContacts{ get; set; }
[...]

with this collection I would show a column that shows a Count of SellToContacts, that there are children of primary contact.

If a try to debug a collection, I found , correctly, some SellToContacts loaded of several children.

[...]
@(Html.Kendo().Grid<Portale.Web2.Data.Entities.Contact>
    ()
    .Name("contactGrid")
    .DataSource(dataSource => dataSource
    .Ajax()
    .Read(read => read.Action("GetContacts", "Contacts").Data("addInfo"))
    )
    .Columns(columns =>
    {
        columns.Bound(c => c.Date).Format("{0:dd/MM/yyyy}");
        columns.Bound(c => c.Name);
        columns.Bound(c => c.SellToContacts.Count);
 
[...]

Why not appears into a grid?

Where I wrong?

1 Answer, 1 is accepted

Sort by
0
Georgi Denchev
Telerik team
answered on 29 Oct 2020, 11:19 AM

Hello Dario,

 

Thank you for the provided code snippets.

The kendo grid columns can be bound to properties of an object(Model). In this case we are attempting to bind the column to a value of a property, which is an incorrect configuration. There are a few options to solve this issue.

Solution 1 - create a SellToContactsCount property in the Model and obtain the count of the list there. Then bind the column to that property instead of the list.

public List<Contact> SellToContacts{ get; set; }

public int SellToContactsCount
{
    get
    {
         return this.SellToContacts.Count;
    }
}

And then in the grid:

columns.Bound(c => c.SellToContactsCount);

Another solution is to use a Column Client Template like so:

columns.Bound(c => c.SellToContacts).ClientTemplate("#= SellToContacts.length #");

Let me know if you require further assistance.

 

Best Regards,
Georgi Denchev
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Grid
Asked by
Dario
Top achievements
Rank 1
Veteran
Answers by
Georgi Denchev
Telerik team
Share this question
or