C Sharp中列值的总和 [英] sum of column values in c sharp

查看:136
本文介绍了C Sharp中列值的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在winform应用程序中,我具有datagridview,其中具有GoodsName列和Price列.我想知道如何制作新行,以便可以看到其中的价格总和.
任何帮助将不胜感激

In the winform app i have datagridview which has GoodsName column and Price column. I want to know how can i make a new row so that i can see the sum of price in it.
Any help will be appreciated

推荐答案


检查此
Hi ,
Check this
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        var result = from x in db.tests
                     select x;

        GridView1.DataSource = result;
        GridView1.DataBind();
    }



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

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
         sum += Convert.ToInt32(((Label)e.Row.FindControl("lbl2")).Text);
    }
    if (e.Row.RowType == DataControlRowType.Footer)
    {
        ((Label)e.Row.FindControl("lblCalc")).Text = sum.ToString();
    }
}







<div>
       <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

           onrowdatabound="GridView1_RowDataBound" ShowFooter="True">
           <Columns>
               <asp:TemplateField HeaderText="ID">
                   <ItemTemplate>
                       <asp:Label ID="Label1" runat="server" Text="<%# Bind('id') %>"></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="calc">
                   <FooterTemplate>
                       <asp:Label ID="lblCalc" runat="server" Text="Label"></asp:Label>
                   </FooterTemplate>
                   <ItemTemplate>
                       <asp:Label ID="lbl2" runat="server" Text="<%# Bind('company') %>"></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
           </Columns>
       </asp:GridView>
   </div>


最好的问候
M.Mitwalli


Best Regards
M.Mitwalli


我希望这对您有所帮助:

摘要DataGridView [
I hope This will help u:

Summary DataGridView[^]


下面的代码示例演示如何在GridView页脚中显示文本.

步骤:

1)将GridView ShowFooter属性设置为true

2)在GridView_RowdataBound事件中编写以下代码


The following code sample shows how to display text in GridView Footer.

Steps:

1) Set GridView ShowFooter property to true

2) Write the below code in GridView_RowdataBound Event


if ( e.Row.RowType == DataControlRowType.DataRow )
   {
            sum = sum + Convert.ToInt32 ( e.Row.Cells [ 0 ].Text );
   }

        else if ( e.Row.RowType == DataControlRowType.Footer )
   {
            e.Row.Cells [ 0 ].Text = sum.ToString ( "c" );
   }


这篇关于C Sharp中列值的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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