我有GridView
,我想添加PremiseScore得分(第三栏)的页脚总和。
我怎么能这样做?
<asp:BoundField DataField="PremiseUno" HeaderText='<%$ Resources:Language, grdPremiseUno %>' ReadOnly="True" SortExpression="PremiseUno" >
<HeaderStyle CssClass="gHeaderStyle" />
<ItemStyle CssClass="gControlStyle" />
</asp:BoundField>
<asp:BoundField DataField="PremiseName" HeaderText='<%$ Resources:Language, grdPremisesName %>'
ReadOnly="True" SortExpression="grdPremisesName" >
<HeaderStyle CssClass="gHeaderStyle" />
<ItemStyle CssClass="gControlStyle" />
</asp:BoundField>
<asp:BoundField DataField="PremiseScore" HeaderText='<%$ Resources:Language, grdPremiseScore %>' ReadOnly="True" SortExpression="PremiseScore" >
<HeaderStyle CssClass="gHeaderStyle" />
<ItemStyle CssClass="gControlStyle" />
</asp:BoundField>
<asp:TemplateField ShowHeader="False" HeaderText= '<%$ Resources:Language, btnDelete %>'>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete" CommandArgument='<%# Eval("PremiseUno") %>' onclick="LinkButton1_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
答案 0 :(得分:2)
如果将绑定字段转换为模板字段,则可以访问包含前提评分值的控件:
<asp:TemplateField HeaderText="test">
<ItemTemplate>
<asp:Label runat="server" ID="testLabel" Text='<%# Eval("PremiseScore") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
然后,您可以执行以下操作来计算gridview的Databound事件中的总和:
protected void Grid_DataBound(Object sender, EventArgs e)
{
GridViewRow footerRow = grid.FooterRow;
var sum = (from GridViewRow row in grid.Rows select ((Label)row.FindControl("testLabel")).Text).Sum(d => Convert.ToInt16(d));
footerRow.Cells[0].Text = sum.ToString();
}
我在这里假设所有值都是整数,但它很容易转换为其他值类型。