Click here to Skip to main content
14,870,759 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello Friends,

i have create an dynamic grid view with 3 text box named 1) item name, 2)qty , 3)price. now i want to get Total of all the price text box in label.

here is code for dynamic add new row in gridview :-
C#
private void AddNewRowToGrid()
    {
        try
        {
            int rowIndex = 0;

            if (ViewState["CurrentTable"] != null)
            {
                DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
                DataRow drCurrentRow = null;
                if (dtCurrentTable.Rows.Count > 0)
                {
                    for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                    {
                        //extract the TextBox values
                        TextBox box1 = (TextBox)GrdProduct.Rows[rowIndex].Cells[1].FindControl("txtitemname");
                        TextBox box2 = (TextBox)GrdProduct.Rows[rowIndex].Cells[2].FindControl("txtqty");
                        TextBox box3 = (TextBox)GrdProduct.Rows[rowIndex].Cells[3].FindControl("txtprice");

                        drCurrentRow = dtCurrentTable.NewRow();

                        drCurrentRow["RowNumber"] = i + 1;
                        drCurrentRow["Column1"] = box1.Text;
                        drCurrentRow["Column2"] = box2.Text;
                        drCurrentRow["Column3"] = box3.Text;

                        rowIndex++;
                    }
                    dtCurrentTable.Rows.Add(drCurrentRow);
                    ViewState["CurrentTable"] = dtCurrentTable;

                    GrdProduct.DataSource = dtCurrentTable;
                    GrdProduct.DataBind();
                }
            }
            else
            {
                Response.Write("ViewState is null");
            }

            //Set Previous Data on Postbacks
            SetPreviousData();
        }
        catch (Exception ex)
        {
            lblerror.Visible = true;
            lblerror.InnerText = ex.Message.ToString();
        }
    }


now i want Total of all the Price in one Label when textbox_changed event fire.

Thanx to all...!!!!
Posted

C#
function calculateamount() {
      var txtTotal = 0;
        var passed = false;
        total = 0.00;
        var id = 0;
        totalDTH = 0.00;
        totalMCF = 0;
        // Get the gridview
        var grid = document.getElementById("<%= GridView1.ClientID%>");
       
        // Get all the input controls (can be any DOM element you would like)
        var inputs = grid.getElementsByTagName("input");
        
        // Loop through all the DOM elements we grabbed
        for (var i = 0; i < inputs.length; i++) {
            // In this case we are looping through all the Dek Volume and then the Mcf volume boxes in the grid and not an individual one and totalling them
            if (inputs[i].name.indexOf("lbl_amt") > 1) {
                if (inputs[i].value != "") {
                    totalDTH = totalDTH + MathRound(parseFloat(inputs[i].value));
                }
            }
        }
        document.getElementById("<%= txtamttotal.ClientID %>").value = MathRound(totalDTH);
        
    }


if (inputs[i].name.indexOf("lbl_amt") > 1) // You should use here textbox of price which is in gridview.

call calculateamount() function on keyup or onblur


please vote if it helps you thanks
   

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