asp.net - Using databound controls to obtain sum from column in related entity in Databound Gridview -


i have gridview bound datasoure on page load. datasource connected various other database tables, ie. datasourceitem.relatedentity

there column in gridview value dependent upon sum of field in related relatedentities.

so datasourceitem has 1 many relationship relatedentity, , need sum value specific column in related relatedentities. want possible, , know syntax wrong, kind of wanted 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 = ds in datacontext.datasource                         ds.id == selectid                         select ds; mygridview.databind(); 

i want keep amount of code minimum, if @ possible, please me figure out how. clear, line of code want make work this:

'<%# bind("relatedentity.columnname").sum() %>' 

or @ least effect. don't have use sum() method... if there different/better way of handling this, feel free let me know

first need use eval instead of bind. next, need cast evaluated expression entitycollection type

<asp:label id="lblsum" runat="server" text='<%# ((system.data.objects.dataclasses.entitycollection<relatedentityitemtype>)eval("relatedentity")).sum(i=>i.columnname) %>'></asp:label> 

also need proper imports <%@ import namespace="yourentitiesnamespace" %> , system.data.entity

edit: if page doesn't compile, needed in web.config

<compilation targetframework="4.0">      <assemblies>      <add assembly="system.data.entity, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"/>                   </assemblies> </compilation> 

also can rid off full name of type using import directive

<%@ import namespace="..entities.." %> <%@ import namespace="system.data.objects.dataclasses" %> 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -