gridview column sum value, and display on webpage asp.net

孤街浪徒 提交于 2020-01-30 08:11:27

问题


I currently have :

<asp:GridView ID="BalanceCheckDataGridView" runat="server"  AutoGenerateColumns="false">
  <Columns>
         <asp:BoundField ItemStyle-Width="30%" DataField="Company" HeaderText="Company" />
         <asp:BoundField ItemStyle-Width="30%" DataField="Balance" HeaderText="Balance" />
   </Columns>
</asp:GridView>

Now I want to grab information from the balance, and sum them up, then display it ( can be at the end a row like "Total: sumValue" or a label where its text can be it's sum value)

I am new to this, Could you help please.

Thanks a lot.


回答1:


Int32 tot = 0; 
protected void Dg_Source_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        tot = tot + Convert.ToInt32(e.Row.Cells[1].Text);
        lblSum.Text = tot.ToString();
    }
}



回答2:


Put a footer on the grid and then you could do something like this in your code behind after your grid is bound.

((Label)your_grid.FooterRow.Cells[1].FindControl("your_label_to_diplay_total")).Text = "Total:" + ds.Tables[0].Compute("sum(your_balance_field)", "").ToString();

This is off the top of my head. You will have to customize it to your application.




回答3:


    int sum=0;
    protected void GridView2_RowDataBound(object sender,  GridViewRowEventArgs e)
         {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label salary = (Label)e.Row.FindControl("Label3");//take lable id
            sum +=int.Parse(salary.Text);
            lblsum.Text = sum.ToString();
        }
    }


来源:https://stackoverflow.com/questions/20251987/gridview-column-sum-value-and-display-on-webpage-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!