I am working with three tables in a db. The first table is labeled ‘Accounts’ and has a one-many relationship with the second, ‘AccountItems’.The AccountItems table has a size column. The third table, somewhat tangential to this question, is ‘Customer’ which has many accounts.
I am Creating a RadGrid that is bound to the Account table. It should look something like this:
Customer | Location | Account# | Total Size
___________________________________________
Location and Account # are easy, as was Customer because the navigation property worked in an item template.
My problem however, is the Size column. It should total the Size from each AccountItem. Example, if there are four AccountItems with a size of ’50’ each, then the total should be 200. It does not seem like I can simply navigate the child relationship in an Eval() like I did for the Customer. Is it possible to do this in the column code, or do I need to do a server side event handler for OnItemCreated?
Edit
I figured it out for a temp fix.
<ItemTemplate>
<asp:Label ID="lblSizeSum" runat="server"Text='<%# CalculateSizeTotal((MS_Accounts)Container.DataItem)%>'>
<ItemTemplate>
And then the code behind:
Protected string CalculateSizeTotal(MS_Accounts dataItem)
{
return dataItem.MS_AccountLoads.Sum(s => s.Size).ToString();
}
The only thing I don’t like about this is the clear violation of MVC by accessing my ORM directly for the casting.
Please check below code snippet.
Let me know if i am missing any thing or not understand your requirement.
.aspx
.aspx.cs
public partial class forumpage : System.Web.UI.Page
{
protected void RadGrid2_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
RadGrid2.DataSource = getDatarofGrid();
}
}
public class Customer
{
public int CustomerID { get; set; }
public List sizes { get; set; }
}
public class Sizes
{
public int size { get; set; }
}