如何获得asp.net的gridview行值总和 [英] How to get total of gridview row values asp.net

查看:91
本文介绍了如何获得asp.net的gridview行值总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得grid.net行值的总和asp.net,



我想在gridview中计算每行的总数。



我试试这段代码不能正常工作



  protected   void  TAXGridview0_RowCreated( object  sender,GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
int total = Convert.ToInt32(e.Row.Cells [ 3 ]。Text)+ Convert.ToInt32(e .Row.Cells [ 4 ]。文本)+ Convert.ToInt32(e.Row.Cells [ 5 ] .Text)+ Convert.ToInt32(e.Row.Cells [ 6 ]。文本)+ Convert.ToInt32(e.Row.Cells [ 7 ]。文本)+ Convert.ToInt32(e.Row.Cells [ 8 ]。文本)+ Convert.ToInt32(e .Row.Cells [ 9 ]。文字);

((标签)TAXGridview.FindControl( Label1))。 Text = Convert.ToString(total);

}

}







any一个人可以帮助我,谢谢你



没有anser我错了,

解决方案

既然你是没有为每一行添加总值,而只是添加一行,这就是为什么你得到的结果只是最后一行。



请看下面的代码,可以帮助你。



  decimal  sumFooterValue =  0 ; 
protected void GridView1_RowDataBound( object sender,GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
string sponsorBonus =((Label)e.Row.FindControl( Label2\" 的))的文本。
string pairingBonus =((Label)e.Row.FindControl( LABEL3\" ))文本;
string staticBonus =((Label)e.Row.FindControl( Label4\" ))文本;
string leftBonus =((Label)e.Row.FindControl( Label5\" ))文本;
string rightBonus =((Label)e.Row.FindControl( Label6\" ))文本;
decimal totalvalue = Convert.ToDecimal(sponsorBonus)+ Convert.ToDecimal(pairingBonus)+ Convert.ToDecimal(staticBonus)+ Convert.ToDecimal(leftBonus)+ Convert .ToDecimal(rightBonus);
e.Row.Cells [ 6 ]。Text = totalvalue.ToString();
sumFooterValue + = totalvalue
}

if (e.Row.RowType == DataControlRowType.Footer)
{
Label lbl =(Label)e.Row.FindControl( lblTotal );
lbl.Text = sumFooterValue.ToString();
}

}


访问这里...



http://www.aspdotnet -suresh.com/2011/02/how-to-display-sum-of-columns-total-in.html [ ^ ]







http://www.dotnetfunda.com/forums/show/3640/calculate-running-total-in-a-gridview-and-displayed-in-each -row [ ^ ]


我建议你  添加一个更多  你的gridveiw即

然后在 gridveiw DataBound事件中编写代码

int total = 0;
for int i = 0; i< grid.Rows.Count)
{
for int j = o; j< grid.Columns.Count)
{
total = total + int.Parse(grid.Rows [i] .Cell [j]。 Text );

}
grid.Rows [i] .Cell [ Total].Text=total;
}


然后 获取值< span class =code-keyword>来自
网格显示 pre>

How to get total of gridview row values asp.net,

I Want to gat each of row total in my gridview.

I try this code it's not working

protected void TAXGridview0_RowCreated(object sender, GridViewRowEventArgs e)
       {

        if (e.Row.RowType == DataControlRowType.DataRow)
          {
             int total = Convert.ToInt32(e.Row.Cells[3].Text) +                  Convert.ToInt32(e.Row.Cells[4].Text) + Convert.ToInt32(e.Row.Cells[5].Text) + Convert.ToInt32(e.Row.Cells[6].Text) + Convert.ToInt32(e.Row.Cells[7].Text) + Convert.ToInt32(e.Row.Cells[8].Text) + Convert.ToInt32(e.Row.Cells[9].Text);

            ((Label)TAXGridview.FindControl("Label1")).Text = Convert.ToString(total);

           }

       }




any one can help me,Thank you

no anser y am I wrong,

解决方案

Since you are not adding value to total for every row, instead you are just doing the add of the one row thats why the result you are getting is of final row only.

Please look the below code that will help you.

decimal sumFooterValue = 0;
  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
         string sponsorBonus = ((Label)e.Row.FindControl("Label2")).Text;
         string pairingBonus = ((Label)e.Row.FindControl("Label3")).Text;
         string staticBonus = ((Label)e.Row.FindControl("Label4")).Text;
         string leftBonus = ((Label)e.Row.FindControl("Label5")).Text;
         string rightBonus = ((Label)e.Row.FindControl("Label6")).Text;
         decimal totalvalue = Convert.ToDecimal(sponsorBonus) + Convert.ToDecimal(pairingBonus) + Convert.ToDecimal(staticBonus) + Convert.ToDecimal(leftBonus) + Convert.ToDecimal(rightBonus);
         e.Row.Cells[6].Text = totalvalue.ToString();
        sumFooterValue += totalvalue
        }

    if (e.Row.RowType == DataControlRowType.Footer)
        {
           Label lbl = (Label)e.Row.FindControl("lblTotal");
           lbl.Text = sumFooterValue.ToString();
        }

   }


visit here...

http://www.aspdotnet-suresh.com/2011/02/how-to-display-sum-of-columns-total-in.html[^]

or

http://www.dotnetfunda.com/forums/show/3640/calculate-running-total-in-a-gridview-and-displayed-in-each-row[^]


I suggest you to add one more column in your gridveiw Namely Total

then write the code in gridveiw DataBound event like

int total=0;
for (int i=0;i<grid.Rows.Count)
{
   for(int j=o;j<grid.Columns.Count)
   {
      total=total+int.Parse(grid.Rows[i].Cell[j].Text);
      
   }
grid.Rows[i].Cell["Total"].Text=total;
}


then fetch the value from grid and display where you want


这篇关于如何获得asp.net的gridview行值总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆