Gridview groups similar rows in one

I have a gridview associated with a table in a database using EntityDataSource . I fill the gridview with only elements in db that match the where clause.

 FRUIT COLOR NUMB.SEEDS ORANGE ORANGE 3 APPLE RED 4 ORANGE ORANGE 2 STRAWBERRY RED 0 

I want to summarize similar lines with respect to NUMB.SEEDS, for example:

 FRUIT COLOR NUMB.SEEDS ORANGE ORANGE 5 APPLE RED 4 STRAWBERRY RED 0 

Any hint please? I don’t know where to start.

Here I put the where clause:

 <asp:EntityDataSource ID="MyEfDataSource" runat="server" ContextTypeName="MyContext" EntitySetName="Fruits" EntityTypeFilter="Fruit" AutoPage="true" AutoSort="true" Where="it.idName = @idName"> <WhereParameters> <asp:ControlParameter ControlID="ddlFruit" PropertyName="SelectedValue" Type="Int32" DefaultValue="0" Name="idName" /> </WhereParameters> </asp:EntityDataSource> 
+4
source share
2 answers

in SQL, you should use the GROUP BY . I think this should work in EntityDataSource :

 <asp:EntityDataSource ID="MyEfDataSource" runat="server" ContextTypeName="MyContext" EntitySetName="Fruits" EntityTypeFilter="Fruit" AutoPage="true" AutoSort="true" Select="it.FRUIT, it.COLOR, SUM(it.[NUMB.SEEDS])" Where="it.idName = @idName" GroupBy"it.FRUIT, it.COLOR" > <WhereParameters> <asp:ControlParameter ControlID="ddlFruit" PropertyName="SelectedValue" Type="Int32" DefaultValue="0" Name="idName" /> </WhereParameters> </asp:EntityDataSource> 

Please note that I invoke your fields in the "Select" and "Where" expressions based on the columns that you entered in your question. They may differ in your actual data source.

+1
source

You can use the QueryCreated event.

 <asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=AdventureWorksEntities" DefaultContainerName="AdventureWorksEntities" EnableFlattening="False" EntitySetName="FRUITS" onquerycreated="EntityDataSource1_QueryCreated"> </asp:EntityDataSource> protected void EntityDataSource1_QueryCreated(object sender, QueryCreatedEventArgs e) { var fruits = e.Query.Cast<Fruits>(); e.Query = fruits.GroupBy(fruit => fruit.COLOR) .Select(group => new { FRUIT, COLOR, group.Sum(fruit => fruit.SEEDS)});; } 

Refer: Applying LINQ Queries to an EntityDataSource

+1
source

All Articles