我有以下代码来显示数据列表。我需要计算总数。
<asp:GridView ID="gvS" runat="server" DataKeyNames="DateCheckIn" AutoGenerateColumns="false" AllowPaging="false" ShowFooter="true" CssClass="table table-striped table-bordered table-hover table-checkable dataTable no-footer" EmptyDataText="No bookings found." OnRowDataBound="gvS_RowDataBound">
<FooterStyle Font-Bold="true" />
<Columns>
<asp:TemplateField HeaderText="Item" ItemStyle-Wrap="false" ItemStyle-Font-Size="12px">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Check-In Date / Payment Date" DataField="DateCheckIn" DataFormatString="{0:dd-MMM-yyyy}" ItemStyle-Font-Size="12px" />
<asp:TemplateField HeaderText="Room Sales" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="right" ItemStyle-Font-Size="12px" FooterStyle-HorizontalAlign="Right">
<ItemTemplate>
<asp:Label runat="server" ID="lRoom" Text='<%# string.Format("{0:#,0.00}", Eval("Room")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="POS" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="right" ItemStyle-Font-Size="12px" FooterStyle-HorizontalAlign="Right">
<ItemTemplate>
<asp:Label runat="server" ID="lPos" Text='<%# string.Format("{0:#,0.00}", Eval("Pos")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Shower" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="right" ItemStyle-Font-Size="12px" FooterStyle-HorizontalAlign="Right">
<ItemTemplate>
<asp:Label runat="server" ID="lShower" Text='<%# string.Format("{0:#,0.00}", Eval("Shower")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="right" ItemStyle-Font-Size="12px" FooterStyle-HorizontalAlign="Right">
<ItemTemplate>
// Total = lRoom + lPos + lShower
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
我需要计算总计(在右侧)等于lRoom + lPos + lShower。我设法获得页脚的总计。
以下是我的gvS_rowDataBound
protected void gvS_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lRoom = (Label)e.Row.FindControl("lRoom");
Label lPos = (Label)e.Row.FindControl("lPos");
Label lShower = (Label)e.Row.FindControl("lShower");
decimal amount = 0;
if (decimal.TryParse(lRoom.Text, out amount))
totalRoomSales += amount;
if (decimal.TryParse(lPos.Text, out amount))
totalPosSales += amount;
if (decimal.TryParse(lShower.Text, out amount))
totalShowerSales += amount;
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[2].Text = string.Format("{0:#,0.00}", totalRoomSales);
e.Row.Cells[3].Text = string.Format("{0:#,0.00}", totalPosSales);
e.Row.Cells[4].Text = string.Format("{0:#,0.00}", totalShowerSales);
}
}
输出应该是这样的
------------------------------------------------------------
| Item | Check-In Date | Room Sales | POS | Shower | Total |
------------------------------------------------------------
| 1 | 01/01/2019 | 100.00 | 5.00| 8.00 | 113.00|
| 2 | 02/01/2019 | 50.00 | 2.00| 3.50 | 55.50|
------------------------------------------------------------
| | | 150.00 | 7.00|11.50 | 168.50|
------------------------------------------------------------
请帮我获取总数。.谢谢
答案 0 :(得分:1)
尝试这样的事情:
<asp:TemplateField HeaderText="Total" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="right" ItemStyle-Font-Size="12px" FooterStyle-HorizontalAlign="Right">
<ItemTemplate>
<asp:Label runat="server" ID="lTotal" Text='<%# string.Format("{0:#,0.00}", Convert.ToDouble(Eval("Room")) + Convert.ToDouble(Eval("Pos")) + Convert.ToDouble(Eval("Shower"))) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
C#
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lTotal = (Label)e.Row.FindControl("lTotal");
if (decimal.TryParse(lShower.Text, out amount))
totalSales+= amount;
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[2].Text = string.Format("{0:#,0.00}", totalRoomSales);
e.Row.Cells[3].Text = string.Format("{0:#,0.00}", totalPosSales);
e.Row.Cells[4].Text = string.Format("{0:#,0.00}", totalShowerSales);
e.Row.Cells[5].Text = string.Format("{0:#,0.00}", totalSales);
}