Displaying Total in Footer of GridView and also Add Sum of columns(row vise) in last Column

前端 未结 4 1651
离开以前
离开以前 2020-12-07 01:12

In my Asp.net App, i have a GridView and i generate the data of column[6] by myself using code behind.

by looking at the code below, i have

4条回答
  •  Happy的楠姐
    2020-12-07 01:42

    Sample Code: To set Footer text programatically

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
          if (e.Row.RowType == DataControlRowType.Footer)
          {
             Label lbl = (Label)e.Row.FindControl("lblTotal");
             lbl.Text = grdTotal.ToString("c");
          }
       }
    

    UPDATED CODE:

      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();
            }
    
       }
    

    In .aspx Page

     
            
            
                
                    
                        
                    
                    
                        
                    
                
                
                    
                        
                    
                    
                        
                    
                
                
                    
                        
                    
                    
                        
                    
                
                
                    
                        
                    
                    
                        
                    
                
                
                    
                        
                    
                    
                        
                    
                
                
                    
                        
                    
                    
                        
                    
                
                
                    
                        
                    
                    
                        
                    
                    
                        
                    
                    
    
                
            
            
            
            
            
            
                        
        
    

    My Blog - Asp.net Gridview Article

提交回复
热议问题