Increment decrement TextBox value inside GridView using C# and VB.Net in ASP.Net

Last Reply 9 months ago By pandeyism

Posted 9 months ago

My question is that on btnPlus or btnMinus Click the quantity in textbox How would increase or decrease quantity.

I need this on webform asp.net c#

here is image for your consideration.

https://ibb.co/7yGgSkv

Posted 9 months ago

Hi akhter,

Refer below sample.

HTML

<asp:GridView runat="server" AutoGenerateColumns="false" ID="GridView1">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:TemplateField HeaderText="Minus">
            <ItemTemplate>
                <asp:Button Text="-" runat="server" OnClick="CalculateMinus" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="QTY">
            <ItemTemplate>
                <asp:TextBox runat="server" ID="txtQuatntity" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Plus">
            <ItemTemplate>
                <asp:Button ID="Button1" Text="+" runat="server" OnClick="CalculatePlus" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Price" HeaderText="Price" />
        <asp:TemplateField HeaderText="Total">
            <ItemTemplate>
                <asp:Label ID="lblTotal" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Namespaces

C#

using System.Data;

VB.Net

Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[] { new DataColumn("Name", typeof(string)), new DataColumn("Price", typeof(int)) });
        dt.Rows.Add("Normal Wax- Full Hands", 100);
        dt.Rows.Add("Normal Wax- Half Hands", 100);
        dt.Rows.Add("Normal Wax- Neck", 200);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}

protected void CalculateMinus(object sender, EventArgs e)
{
    Button btn = (sender) as Button;
    GridViewRow row = (GridViewRow)btn.NamingContainer;
    int qty = Convert.ToInt16((row.FindControl("txtQuatntity") as TextBox).Text);
    int price = Convert.ToInt16(row.Cells[4].Text);
    (row.FindControl("lblTotal") as Label).Text = (qty - price).ToString();
}

protected void CalculatePlus(object sender, EventArgs e)
{
    Button btn = (sender) as Button;
    GridViewRow row = (GridViewRow)btn.NamingContainer;
    int qty = Convert.ToInt16((row.FindControl("txtQuatntity") as TextBox).Text);
    int price = Convert.ToInt16(row.Cells[4].Text);
    (row.FindControl("lblTotal") as Label).Text = (qty + price).ToString();
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As DataTable = New DataTable()
        dt.Columns.AddRange(New DataColumn() {New DataColumn("Name", GetType(String)), New DataColumn("Price", GetType(Integer))})
        dt.Rows.Add("Normal Wax- Full Hands", 100)
        dt.Rows.Add("Normal Wax- Half Hands", 100)
        dt.Rows.Add("Normal Wax- Neck", 200)
        GridView1.DataSource = dt
        GridView1.DataBind()
    End If
End Sub

Protected Sub CalculateMinus(ByVal sender As Object, ByVal e As EventArgs)
    Dim btn As Button = TryCast((sender), Button)
    Dim row As GridViewRow = CType(btn.NamingContainer, GridViewRow)
    Dim qty As Integer = Convert.ToInt16((TryCast(row.FindControl("txtQuatntity"), TextBox)).Text)
    Dim price As Integer = Convert.ToInt16(row.Cells(4).Text)
    TryCast(row.FindControl("lblTotal"), Label).Text = (qty - price).ToString()
End Sub

Protected Sub CalculatePlus(ByVal sender As Object, ByVal e As EventArgs)
    Dim btn As Button = TryCast((sender), Button)
    Dim row As GridViewRow = CType(btn.NamingContainer, GridViewRow)
    Dim qty As Integer = Convert.ToInt16((TryCast(row.FindControl("txtQuatntity"), TextBox)).Text)
    Dim price As Integer = Convert.ToInt16(row.Cells(4).Text)
    TryCast(row.FindControl("lblTotal"), Label).Text = (qty + price).ToString()
End Sub

Screenshot