Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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?

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 cheked second item in cheboxlist then second column added int gridview as shown in my figure

http://i.stack.imgur.com/WOzgD.png[^]

my desired result (output)
http://i.stack.imgur.com/tqMav.png[^]

my c# code.
C#
  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 < GridView1.Rows.Count; i++)
     {

         int j;
         double Total = 0.00;
         for (j = 0; j < GridView1.Columns.Count; j++)
         {

             Total += GridView1.Rows[i].Cells[j].Text != "&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");


         }

       }

    }
}



my aspx code

ASP.NET
<div style="float:left">
                        <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">
                            <RowStyle ForeColor="#333333" BackColor="White" Height="20px" />
                            <Columns>
                               <asp:TemplateField>
                                    <HeaderTemplate>Sr.No </HeaderTemplate>
                                    <ItemTemplate>  <%#Container.DataItemIndex+1 %></ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField>
                                    <HeaderTemplate>Fee Name </HeaderTemplate>
                                    <ItemTemplate>
                                    <asp:Label ID="lblfeename" runat="server" Text='<%#Eval("feename") %>'></asp:Label>
                                    </ItemTemplate>
                                    <FooterTemplate> <asp:Label ID="lblfeename" CssClass="footer_text" runat="server" Text="Total"></asp:Label></FooterTemplate>
                                </asp:TemplateField>
                           </Columns>
                            <FooterStyle BackColor="White" ForeColor="#333333" />
                            <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
                            <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
                            <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
                        </asp:GridView>
                    </div>
                    <div>
                        <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">
                            <RowStyle ForeColor="#333333" BackColor="White" Height="21px" />
                            <Columns>

                            </Columns>
                            <FooterStyle BackColor="White" ForeColor="#333333" Font-Bold="true" />
                            <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
                            <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
                            <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
                        </asp:GridView>

                        </div>



       <div class="duedateheading">
                 <asp:Label ID="lbl_formonth" runat="server" Font-  Bold="True" Text="For Month"></asp:Label>
                         </div>
                 <asp:CheckBoxList ID="chbxlstduedate" runat="server"        AutoPostBack="true" onselectedindexchanged="chbxlstduedate_SelectedIndexChanged">
                 </asp:CheckBoxList>
                         </div>

it's urgent for me please any one can give some idea or suggestion ..
thanks
Posted
Updated 21-Jun-16 0:04am
v2

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