如何从GridView获取总计并将其放在页面上的其他位置?
我有一个网页,其中包含多个数据绑定的GridView
控件.在这些网格的RowDataBound
事件中,我累加了总计,如下所示:
I have a web page with several databound GridView
controls. In the RowDataBound
events for these grids, I accumilate the gross total, like this:
Protected Sub TradesGrid_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim DVR As DataRowView = CType(e.Row.DataItem, DataRowView)
If IsNumeric(DVR("Concession")) Then
Gross += CDec(DVR("Concession"))
End If
End If
End Sub
渲染完所有网格后,我需要将最终的总计放在网格上方的页面顶部.
我的问题似乎是在我最后一次手动更改页面内容的机会之后,RowDataBound
事件似乎在页面渲染期间被激发了.即使在PreRenderComplete
事件期间,Gross
仍为零.
我是在做错什么,还是我必须告诉管理层他们可以在每个网格中汇总(工作正常),但不能在网格外部进行汇总?
Once all of the grids have been rendered, I need to put the final gross total at the top of the page, above the grids.
My problem seems to be that the RowDataBound
event seems to get fired during the page''s render, after my last opportunity to manually change the page''s content; even during the PreRenderComplete
event, Gross
is still zero.
Am I doing something wrong, or will I have to tell management that they can summaries in each grid (which is working fine) but not a full total outside of the grids?
Dude,您可以尝试辅助函数(在代码后面).您可以为All gridview total&创建另一个函数.加载所有gridviews后在codebehind中调用它.试试这个
如何在GridView控件的页脚中显示总计 "_blank" title =新窗口"> ^ ]
编辑
--------------------------------------
你可以这样
Dude, you can try helper functions(in codebehind). You can create another function for All gridview total & call that in codebehind after loading all the gridviews. Try this
How to Display Sum Total in the Footer of the GridView Control[^]
EDIT
--------------------------------------
You can do this way
public partial class _Default : System.Web.UI.Page
{
decimal grdTotal1 = 0;//GridView1 Total
decimal grdTotal1 = 0;//GridView2 Total
protected void Page_Load(object sender, EventArgs e)
{
//After GridView bindings
lblAllGridViewTotal.Text = (grdTotal1 + grdTotal2).ToString();//Displaying All Total value in a label top of the page
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
decimal rowTotal = Convert.ToDecimal
(DataBinder.Eval(e.Row.DataItem, "Amount"));
grdTotal1 = grdTotal1 + rowTotal;//GridView1 Total
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lbl = (Label)e.Row.FindControl("lblTotal");
lbl.Text = grdTotal1.ToString("c");
}
}
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
decimal rowTotal = Convert.ToDecimal
(DataBinder.Eval(e.Row.DataItem, "Amount"));
grdTotal2 = grdTotal2 + rowTotal;//GridView2 Total
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lbl = (Label)e.Row.FindControl("lblTotal");
lbl.Text = grdTotal2.ToString("c");
}
}
}
在ASP.NET C#VB.NET中的Gridview页脚中运行总计 [ ^ ]
顺便说一句,您只需要稍稍更改代码即可(您可以将每个摘要值存储在会话,视图状态等状态下,然后可以对所有值求和并得到结果.).请让我知道.
Running Total In Gridview Footer in ASP.NET C# VB.NET[^]
BTW you just need to change the code little bit(You can store the every summary values in state like session, viewstate, etc., then you can sum all values & you can get the result.). Let me know please.
也许如果您使用ItemTemplate来获取总数(如此处所示) [ ^ ]),它可能会对您有所帮助.
Maybe if you use the ItemTemplate to get the total (as shown here[^]), it may help you.