title: How to calculate the sum of particular rows and show in another column in gridview using C# .net?
body: <p>I am working on a school ERP project as an ASP.NET web application in which we have a gridview. On my page I want to calculate the sum of cells value row wise  of the gridview but the code which I am using returns two or three times the total of the particular column. Any suggestions on how we can calculate the right total of the particular column?</p>
  
  <p>Note: Here we have one checkbox list which fill from database and the item of chekboxlist are the column of gridview when we checked an item in chekboxlist a column dynamically add gridview and when we checked second item in cheboxlist then second column added int gridview as shown in my figure </p>
  
  <p><a href="https://i.stack.imgur.com/WOzgD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WOzgD.png" alt="enter image description here"></a> </p>
  
  <p>my desired result (output)</p>
  
  <p><a href="https://i.stack.imgur.com/tqMav.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tqMav.png" alt="enter image description here"></a></p>
  
  <p>my c# code.</p>
  
  <pre><code>     using System;
       using System.Collections;
       using System.Configuration;
       using System.Data;
       using System.Linq;
       using System.Web;
       using System.Web.Security;
       using System.Web.UI;
       using System.Web.UI.HtmlControls;
       using System.Web.UI.WebControls;
       using System.Web.UI.WebControls.WebParts;
       using System.Xml.Linq;
       using System.Data.SqlClient;
       namespace egurukul
       {
       public partial class feerecipts : System.Web.UI.Page
        {
         decimal val;
        string _connStr =   ConfigurationManager.ConnectionStrings["con1"].ConnectionString;
          protected void Page_Load(object sender, EventArgs e)
            {
               if (!IsPostBack)
                {
  
                 bindgvfeehead();
                 BindTableColumns();
                }
            }
  
         private void BindTableColumns()
          {
  
          DataTable table = new DataTable();
  
          using (SqlConnection conn = new SqlConnection(_connStr))
          {
  
              SqlCommand cmd = new SqlCommand("Readduedat", conn);
  
              cmd.CommandType = CommandType.StoredProcedure;
  
              // get the adapter object and attach the command object to it
  
              SqlDataAdapter ad = new SqlDataAdapter(cmd);
  
  
              // fire Fill method to fetch the data and fill into DataTable
  
              ad.Fill(table);
  
          }
  
          chbxlstduedate.DataSource = table;
          chbxlstduedate.DataTextField = "duemonth";
          chbxlstduedate.DataValueField = "duedateid";
          chbxlstduedate.DataBind();
  
      }
  
      public void bindgvfeehead()
      {
          SqlConnection
          con = new SqlConnection();
          con.ConnectionString =   ConfigurationManager.ConnectionStrings["con1"].ConnectionString;
          con.Open();
          SqlCommand cmd = new SqlCommand();
          cmd.CommandText = "readfeehead";
          cmd.CommandType = CommandType.StoredProcedure;
          cmd.Connection = con;
          cmd.Parameters.AddWithValue("@classname", Convert.ToString(ddlclassname.SelectedItem));
          cmd.Parameters.AddWithValue("@feecatename", Convert.ToString(ddlstcategory.SelectedItem));
          SqlDataAdapter da = new SqlDataAdapter(cmd);
          DataTable table = new DataTable();
          da.Fill(table);
          gvfeehead.DataSource = table;
          gvfeehead.DataBind();
          con.Close();
  
      }
  
      private void GetData()
      {
  
          DataTable table = new DataTable();
  
          // get the connection
  
          SqlConnection conn = new SqlConnection(_connStr);
          conn.Open();
          SqlCommand cmd = new SqlCommand();
          cmd.Connection = conn;
          cmd.CommandText = "readfeedetails";
          cmd.CommandType = CommandType.StoredProcedure;
          // instantiate the command object to fire
          cmd.Parameters.AddWithValue("@classname", Convert.ToString(ddlclassname.SelectedItem));
          cmd.Parameters.AddWithValue("@feecatename", Convert.ToString(ddlstcategory.SelectedItem));
          SqlDataAdapter ad = new SqlDataAdapter(cmd);
          // fire Fill method to fetch the data and fill into DataTable
  
          ad.Fill(table);
          conn.Close();
  
          // specify the data source for the GridView
  
          GridView1.DataSource = table;
  
          // bind the data now
  
          GridView1.DataBind();
      }
  
      protected void chbxlstduedate_SelectedIndexChanged(object sender, EventArgs e)
      {
          foreach (ListItem item in chbxlstduedate.Items)
          {
  
              if (item.Selected)
              {
  
                  BoundField b = new BoundField();
  
                  b.DataField = item.Text;
  
                  b.HeaderText = item.Text;
  
                  GridView1.Columns.Add(b);
  
  
  
              }
  
  
          }
  
          TemplateField tfield = new TemplateField();
          tfield.HeaderText = "Total";
          GridView1.Columns.Add(tfield);
  
          this.GetData();
  
          bindgvfeehead();
      }
  
      protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
      {
  
          for (int i = 0; i &lt; GridView1.Rows.Count; i++)
          {
  
              int j;
              double Total = 0.00;
              for (j = 0; j &lt; GridView1.Columns.Count; j++)
              {
  
                  Total += GridView1.Rows[i].Cells[j].Text != "&amp;nbsp;" ? double.Parse(GridView1.Rows[i].Cells[j].Text) : 0.000;
  
              }
              int k = Convert.ToInt32(GridView1.Columns.Count);
              GridView1.Rows[i].Cells[k - 1].Text = Total.ToString("#0.000");
  
  
              }
  
            }
  
         }
     }
  </code></pre>
  
  <p>my aspx code</p>
  
  <pre><code>&lt;div style="float:left"&gt;
                          &lt;asp:GridView ID="gvfeehead" runat="server" AutoGenerateColumns="False" 
                              BackColor="White" BorderColor="#336666" BorderStyle="Double" 
                              Font-Size="11px"   BorderWidth="3px" 
                              CellPadding="4" GridLines="both" ShowFooter="True"&gt;
                              &lt;RowStyle ForeColor="#333333" BackColor="White" Height="20px" /&gt;
                              &lt;Columns&gt;
                                 &lt;asp:TemplateField&gt;
                                      &lt;HeaderTemplate&gt;Sr.No &lt;/HeaderTemplate&gt;
                                      &lt;ItemTemplate&gt;  &lt;%#Container.DataItemIndex+1 %&gt;&lt;/ItemTemplate&gt;
                                  &lt;/asp:TemplateField&gt;
                                  &lt;asp:TemplateField&gt;
                                      &lt;HeaderTemplate&gt;Fee Name &lt;/HeaderTemplate&gt;
                                      &lt;ItemTemplate&gt;
                                      &lt;asp:Label ID="lblfeename" runat="server" Text='&lt;%#Eval("feename") %&gt;'&gt;&lt;/asp:Label&gt;
                                      &lt;/ItemTemplate&gt;
                                      &lt;FooterTemplate&gt; &lt;asp:Label ID="lblfeename" CssClass="footer_text" runat="server" Text="Total"&gt;&lt;/asp:Label&gt;&lt;/FooterTemplate&gt;
                                  &lt;/asp:TemplateField&gt;
                             &lt;/Columns&gt;
                              &lt;FooterStyle BackColor="White" ForeColor="#333333" /&gt;
                              &lt;PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" /&gt;
                              &lt;SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" /&gt;
                              &lt;HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" /&gt;
                          &lt;/asp:GridView&gt;
                      &lt;/div&gt;
                      &lt;div&gt;
                          &lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                              BackColor="White" BorderColor="#336666" BorderStyle="Double" 
                              Font-Size="11px" BorderWidth="3px" 
                              CellPadding="4" GridLines="both" 
                              ShowFooter="True" EnableViewState="false" 
                              onrowdatabound="GridView1_RowDataBound"&gt;
                              &lt;RowStyle ForeColor="#333333" BackColor="White" Height="21px" /&gt;
                              &lt;Columns&gt;
  
                              &lt;/Columns&gt;
                              &lt;FooterStyle BackColor="White" ForeColor="#333333" Font-Bold="true" /&gt;
                              &lt;PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" /&gt;
                              &lt;SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" /&gt;
                              &lt;HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" /&gt;
                          &lt;/asp:GridView&gt;
  
                          &lt;/div&gt;
  
  
  
         &lt;div class="duedateheading"&gt;
                   &lt;asp:Label ID="lbl_formonth" runat="server" Font-  Bold="True" Text="For Month"&gt;&lt;/asp:Label&gt;
                           &lt;/div&gt;
                   &lt;asp:CheckBoxList ID="chbxlstduedate" runat="server"        AutoPostBack="true" onselectedindexchanged="chbxlstduedate_SelectedIndexChanged"&gt;
                   &lt;/asp:CheckBoxList&gt;
                           &lt;/div&gt;
  </code></pre>