I have a gridview that is bound to a datasoure on page load. The datasource is connected to various other database tables, ie. datasourceItem.relatedEntity
There is a column in the gridview whose value is dependent upon the sum of a certain field in all related relatedEntities
.
So dataSourceItem
has a one to many relationship with relatedEntity
, and I need to sum the value from a specific column in all related relatedEntities
. I want to do this as simply as possible, and I know this syntax is wrong, but this is kind of what I wanted to do:
Markup:
<asp:TemplateField HeaderText="Sum">
<ItemTemplate>
<asp:Label ID="lblSum" runat="server" Text='<%# Bind("relatedEntity.ColumnName").Sum() %>' />
</ItemTemplate>
</asp:TemplateField>
Code-behind (databinding):
myGridview.DataSource = from ds in DataContext.dataSource
where ds.Id == selectId
select ds;
myGridview.DataBind();
I want to keep the amount of code to a minimum, so if this is at all possible, please help me figure out how. To be clear, the line of code I want to make work is this:
'<%# Bind("relatedEntity.ColumnName").Sum() %>'
Or at least something to that effect. I don't necessarily have to use the Sum()
method... if there is a different/better way of handling this, feel free to let me know