我有一个 gridview,它使用 EntityDataSource
绑定(bind)到数据库中的一个表。我仅使用数据库中对应于 where 子句的项目填充 gridview。
FRUIT COLOR NUMB.SEEDS
ORANGE ORANGE 3
APPLE RED 4
ORANGE ORANGE 2
STRAWBERRY RED 0
我想要的是对 NUMB.SEEDS 的相似行求和,如下所示:
FRUIT COLOR NUMB.SEEDS
ORANGE ORANGE 5
APPLE RED 4
STRAWBERRY RED 0
有什么提示吗?我不知道从哪里开始。
这是我放置 where 子句的地方:
<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>
最佳答案
在 SQL 中,您将使用 GROUP BY
子句。我认为这应该适用于 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>
请注意,我是根据您在问题中输入的列来调用“选择”和“Where”表达式中的字段。它们在您的实际数据源中可能有所不同。
关于c# - Gridview 将相似的行归为一组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15159208/