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
<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
<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
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();
}
}