Click here to Skip to main content
16,022,234 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want Calculate sub total similar product type and then Grand Total for all product


I have bind data to grid view using data set but columns in data set are dynamic
i.e
retrieve
from multiple table so number of columns changes depending on condition I have product of type A and B
Suppose I have 5 product of type A and 4 Product of Type B
Now I want Sub total of type A and dynamically and add it to gridview
then for B type product
ant then for total

What I have tried:

Method to bind data to Gridview
protected void GrdLeadsProbabilityDataBind()
      {
          string selectedTreeValue = Session["selectvaluetree"].ToString();
          con.Open();
          SqlDataReader reader7 = new SqlCommand("Select * from User_Master where UserID='" + Session["selectvaluetree"].ToString() + "'", con).ExecuteReader();
          if (reader7.Read())
          {
              Usernamefull = reader7["UserFirstName"].ToString() + ' ' + reader7["UserLastName"].ToString();
              Session["abc1"] = Usernamefull;

          }
          reader7.Close();
          string str3 = "";
          dset = getDataDataset(string.Concat(new object[] { "exec stpr_getUserHierarchy @userid='" + selectedTreeValue + "'" }));
          for (int i = 0; i < dset.Tables[0].Rows.Count; i++)
          {
              str3 = str3 + dset.Tables[0].Rows[i]["userid"].ToString() + ",";
          }
          DataSet dsetProbability;
          dsetProbability = getDataDataset("exec [Proc_LeadsProbability] @OrderList='" + str3.Remove(str3.Length - 1) + "'");

          //dset2 = getDataDataset("exec MIS1stTable_Total @OrderList='" + str3.Remove(str3.Length - 1) + "'");
          Session["TaskTableyo1"] = dsetProbability;

          //GrdProbability.DataSource = dsetProbability;
          ////.Tables[0];


          //GrdProbability.DataBind();


          for (int i = 0; i < dsetProbability.Tables[0].Columns.Count; i++)
          {
              if (dsetProbability.Tables[0].Columns[i].ColumnName == "Parameter")
              {
                  ViewState["CellIndex1"] = i;

              }
              if (dsetProbability.Tables[0].Columns[i].ColumnName == "ProductType")
              {
                  ViewState["CellIndex4"] = i;

              }

          }



          if (dsetProbability != null)
          {
              if (dsetProbability.Tables[0].Rows.Count != 0)
              {
                  GrdProbability.DataSource = dsetProbability.Tables[0];
                  //.Tables[0];
                  GrdProbability.DataBind();
              }
              else
              {
                  GrdProbability.DataSource = null;
                  GrdProbability.DataBind();
              }
          }



          con.Close();
      }


row bound event

protected void GrdProbability_RowDataBound(object sender, GridViewRowEventArgs e)
      {
          if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Footer)
          {

              int index = Convert.ToInt32(ViewState["CellIndex1"]);
              int index1 = Convert.ToInt32(ViewState["CellIndex4"]);

              e.Row.Cells[index].Visible = false;
              e.Row.Cells[index1].Visible = false;
          }

      }



row created Event
protected void GrdProbability_RowCreated(object sender, GridViewRowEventArgs e)
      {

          subTotal = 0;
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
              string str2 = "SME Ratings & Gradings";
              // DataTable dt = (e.Row.DataItem as DataRowView).DataView.Table;
              // int orderId = Convert.ToInt32(dt.Rows[e.Row.RowIndex]["OrderID"]);
              DataTable dt = (e.Row.DataItem as DataRowView).DataView.Table;
              string str = dt.Rows[e.Row.RowIndex]["Product Name"].ToString();
            //  string str1 = dt.Rows[e.Row.RowIndex]["Cold"].ToString();
              string str3 = dt.Rows[e.Row.RowIndex]["Warm"].ToString();
              //int orderId = Convert.ToInt32(dt.Rows[e.Row.RowIndex]["OrderID"]);

              if (dt.Rows[e.Row.RowIndex]["Cold"] == null)
              {
                  dt.Rows[e.Row.RowIndex]["Cold"] = 0;
              }


              total += Convert.ToDecimal(dt.Rows[e.Row.RowIndex]["Cold"]);
              int result = string.CompareOrdinal(str, str2);
              if (result == 0)
              {
                  if (e.Row.RowIndex > 0)
                  {
                      for (int i = subTotalRowIndex; i < e.Row.RowIndex; i++)
                      {
                          subTotal += Convert.ToDecimal(GrdProbability.Rows[i].Cells[3].Text);
                      }
                      this.AddTotalRow("Sub Total", subTotal.ToString("N2"));
                      subTotalRowIndex = e.Row.RowIndex;
                  }
              }
              //{
              //    if (e.Row.RowIndex > 0)
              //    {
              //        for (int i = subTotalRowIndex; i < e.Row.RowIndex; i++)
              //        {
              //            subTotal += Convert.ToDecimal(GridView1.Rows[i].Cells[2].Text);
              //        }
              //        this.AddTotalRow("Sub Total", subTotal.ToString("N2"));
              //        subTotalRowIndex = e.Row.RowIndex;
              //    }
              //    currentId = orderId;
              //}
          }

      }


Method to add row
private void AddTotalRow(string labelText, string value)
       {
           GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
         //  row.BackColor = ColorTranslator.FromHtml("#F9F9F9");
           row.Cells.AddRange(new TableCell[3] { new TableCell (), //Empty Cell
                                       new TableCell { Text = labelText, HorizontalAlign = HorizontalAlign.Right},
                                       new TableCell { Text = value, HorizontalAlign = HorizontalAlign.Right } });

           GrdProbability.Controls[0].Controls.Add(row);
       }


grid view databind method

protected void GrdProbability_DataBound(object sender, EventArgs e)
      {
          for (int i = subTotalRowIndex; i < GrdProbability.Rows.Count; i++)
          {
              subTotal += Convert.ToDecimal(GrdProbability.Rows[i].Cells[3].Text);
          }
          this.AddTotalRow("Sub Total", subTotal.ToString("N2"));
          this.AddTotalRow("Total", total.ToString("N2"));

      }


But it is showing exception
Posted
Comments
BillWoodruff 28-Aug-17 16:15pm    
What exceptions ? Where ?

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