Click here to Skip to main content
15,908,445 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
WHen i enter Product in Gridview and set quantity which i want to sell and it also show sum total in Lable Filed but one i enter second Product It reset Quantity to 0 which i enter before and also reset SUm total Value on Button Click.

What I have tried:

on Page Load
VB
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
   If Not IsPostBack Then
     Dim dt As New DataTable()
            dt.Columns.AddRange(New DataColumn(3) {New DataColumn("ID"), New DataColumn("Item"), New DataColumn("Price"), New DataColumn("txtQuantity")})
            ViewState("Customers") = dt

   End If
End Sub

On Button Click Event
VB
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       Dim dt As DataTable = DirectCast(ViewState("Customers"), DataTable)
       dt.Rows.Add(TextBox2.Text.Trim, itemcode.Trim(), 1000)
       ViewState("Customers") = dt
       GridView1.DataSource = dt
       GridView1.DataBind()
   End Sub

and in Design View
HTML
<asp:GridView ID="GridView1" runat="server" CssClass="table table-responsive table-striped footable" PageSize="20" Style="max-width: 100%" AutoGenerateColumns="false" AutoGenerateDeleteButton="True" OnRowEditing="OnRowEditing" OnRowCancelingEdit="OnRowCancelingEdit" AutoGenerateEditButton="True">
    <Columns>
         <asp:BoundField DataField="ID" HeaderText="ID" ItemStyle-Width="10%"/>
        <asp:BoundField DataField="Item" HeaderText="Item" ItemStyle-Width="50%"/>
        <asp:BoundField DataField="Price" HeaderText="Price" ItemStyle-CssClass="price" ItemStyle-Width="10%"/>

        <asp:TemplateField HeaderText="Quantity" ItemStyle-Width="10%">
            <ItemTemplate>
                <asp:TextBox ID="txtQuantity" runat="server" ></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Total" ItemStyle-Width="20%">
            <ItemTemplate>
                <asp:Label ID="lblTotal" runat="server" Text="0"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
Posted
Updated 16-Sep-20 23:55pm
v2

1 solution

You have not shred the code of OnRowEditing here and seems would be interfering with your logic.

Have a look at the following examples to see how it can be done:
Shopping Cart | Microsoft Docs[^]
Display SubTotal and Grand Total in ASP.Net GridView[^]
Quantity Validation and Total Calculation in ASP.NET Gridview Shopping Cart with jQuery - TechBrij[^]

try out!
 
Share this answer
 
Comments
Naqash Younis 17-Sep-20 6:21am    
this is code
Protected Sub OnRowEditing(sender As Object, e As GridViewEditEventArgs)
GridView1.EditIndex = e.NewEditIndex
Me.BindGrid()
End Sub
Sandeep Mewara 18-Sep-20 1:04am    
Above code is for:triggering edit button inside that row is clicked.
Use debugger and see the execution flow to know more.

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