Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
i have 3 column Price,quantity,and totalPrice in gridview
i have issue when i save all value of gridview price and quantity are save properly but for total price it save only last row value of total_price column

below my code


jquery
C#
<script type="text/javascript">
        $(function () {
            $("[id*=txtQuantity]").val("0");
        });
        $("[id*=txtQuantity]").live("change", function () {
            if (isNaN(parseInt($(this).val()))) {
                $(this).val('0');
            } else {
                $(this).val(parseInt($(this).val()).toString());
            }
        });
        $("[id*=txtQuantity]").live("keyup", function () {
            if (!jQuery.trim($(this).val()) == '') {
                if (!isNaN(parseFloat($(this).val()))) {

                  
                    var row = $(this).closest("tr");
                    $("[id*=lblTotal]", row).html(parseFloat($(".price", row).html()) * parseFloat($(this).val()));
                    var row = $(this).closest("tr");
                    $("input:hidden[id*=MyHidden]").val(parseFloat($(".price", row).html()) * parseFloat($(this).val()));
                
                
                }
                else {
                    var row = $(this).closest("tr");
                    $("[id*=lblTotal]", row).html(parseFloat($(".price", row).html()) * 0);
                }
            } else {
                var row = $(this).closest("tr");
                $("[id*=lblTotal]", row).html(parseFloat($(".price", row).html()) * 0);
                $(this).val('');
            }
            var grandTotal = 0;
            $("[id*=lblTotal]").each(function () {
                grandTotal = grandTotal + parseFloat($(this).html());
            });
            $("[id*=lblGrandTotal]").html(grandTotal.toString());
        });
    </script>



Asp.net

XML
<asp:GridView ID="GridView1" class="sorting" role="columnheader" TabIndex="0" aria-controls="example2"
                                    aria-label=": activate to sort column ascending" CssClass="table table-striped dataTable"
                                    aria-describedby="example2_info" runat="server" AutoGenerateColumns="false">
                                    <Columns>


                                         <asp:TemplateField HeaderText="Product">
                                            <ItemTemplate>
                                                <asp:Label ID="lblProduct" runat="server" Text='<%# Eval("Product_Name") %>'>'></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                         <asp:TemplateField HeaderText="ProductID" Visible="false">
                                            <ItemTemplate>
                                                <asp:Label ID="lblProduct_Id" runat="server" Text='<%# Eval("Product_Id") %>'>'></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:BoundField DataField="Unit_Price" HeaderText="Price" ItemStyle-CssClass="price" />

                                        <asp:TemplateField HeaderText="Quantity">
                                            <ItemTemplate>
                                                <asp:TextBox ID="txtQuantity" runat="server" Text='<%# Eval("ProdQuantity") %>'>' ></asp:TextBox>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Total">
                                            <ItemTemplate>

                                             <asp:Label ID="lblTotal" runat="server" Text="0"></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                    </Columns>
                                </asp:GridView>




<asp:HiddenField ID="MyHidden" runat="server" />

C# code
C#
foreach (GridViewRow row in GridView1.Rows)
      {


          if (row.RowType == DataControlRowType.DataRow)
          {



              Label Product = (Label)row.FindControl("lblProduct");
              Label ProductID = (Label)row.FindControl("lblProduct_Id");

              Label price = (Label)row.FindControl("Unit_Price");
              TextBox Quanty = (TextBox)row.FindControl("txtQuantity");


              string a = MyHidden.Value;

              SqlConnection con = new SqlConnection(StrConnection);
              con.Open();
              SqlCommand cmd = new SqlCommand();
              cmd.Connection = con;
              cmd.CommandType = CommandType.Text;
              cmd.CommandText =
                  "insert into Order_Table(Product_Id,Quantity,Total_Price ) values(@Product,@Quanty,@Total_Price) ";

              cmd.CommandType = System.Data.CommandType.Text;
              cmd.Parameters.Add("@Product", SqlDbType.Char, 20, "Product_Id").Value = ProductID.Text;
              cmd.Parameters.AddWithValue("@Quanty", Quanty.Text);
              cmd.Parameters.AddWithValue("@Total_Price", a);


              cmd.ExecuteNonQuery();

          }
      }
Posted
Updated 27-Sep-17 23:19pm
v3

1 solution

I can clearly see that you are not taking Total_Price value for that row.

It should use:
row.FindControl("YOUR_TOTAL_PRICE_FIELD");

Just like other fields.
 
Share this answer
 
Comments
Rajnish D mishra 5-Dec-14 23:43pm    
afeter u r guidance i taking total_price value in row but it cant happen

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