本文介绍了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

推荐答案

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



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中列值的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 19:22