Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

Hi ,
Check this
C#
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();
    }
}



XML
<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>

Best Regards
M.Mitwalli
 
Share this answer
 
I hope This will help u:

Summary DataGridView[^]
 
Share this answer
 
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


C#
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" );
   }
 
Share this answer
 
Comments
aliprogrammer 6-May-12 16:31pm    
how to show the result in a new row under the intention column?
Member 10659195 17-Apr-14 15:20pm    
i have a gridview bound to database and it has column named product_name,price/unit now i have to provide multiple quantity of such products and multiple products are to be selected by the customer and also show the entire shopping data at a single field with the total amount and amount of each entity
pl. help me out brother

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900