Hi makumbi,
Please refer below sample.
HTML
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Panel ID="Panel1" runat="server">
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:GridView ID="Gvgridclient" runat="server" AutoGenerateColumns="False" ShowFooter="True">
<Columns>
<asp:TemplateField HeaderText="ItemCode">
<ItemTemplate>
<asp:TextBox ID="txtItemCode" runat="server" Text='<%# Bind("ItemCode") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ItemDescription" HeaderText="ItemDescription" />
<asp:BoundField DataField="ItemSize" HeaderText="ItemSize" />
<asp:TemplateField HeaderText="Quantity">
<EditItemTemplate>
<asp:TextBox ID="Quantity" runat="server" Text='<%# Bind("Quantity") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="Fqty" runat="server" Width="46px"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server" AutoPostBack="true" OnTextChanged="quantity_TextChanged" Text='<%# Eval("Quantity") %>' Width="46px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="OrderPrice" HeaderText="OrderPrice" />
<asp:TemplateField HeaderText="SalePrice">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("SalePrice") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("SalePrice") %>' Visible="False"></asp:Label>
<asp:TextBox ID="txtSalePrice" runat="server" Text='<%# Eval("SalePrice") %>' Width="85px" Height="16px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SubTotal">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtFsubtotal" runat="server" Width="46px"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblsubtotal" runat="server" Text='<%# Bind("SubTotal") %>'></asp:Label>
<asp:TextBox ID="txtsubtotal" runat="server" Height="16px" Text='<%# Eval("SubTotal") %>' Width="75px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Prno" HeaderText="Prno" />
<asp:BoundField DataField="Names" HeaderText="Names" />
<asp:BoundField DataField="Date" HeaderText="Date" />
<asp:TemplateField HeaderText="DelSingle">
<ItemTemplate>
<asp:LinkButton ID="lnkRemove" runat="server" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
Namespace
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[11] {
new DataColumn("ItemCode"),
new DataColumn("ItemDescription"),
new DataColumn("ItemSize"),
new DataColumn("Quantity"),
new DataColumn("OrderPrice"),
new DataColumn("SalePrice"),
new DataColumn("SubTotal"),
new DataColumn("Prno"),
new DataColumn("Names"),
new DataColumn("Date"),
new DataColumn("DelSingle")});
dt.Rows.Add("012", "Item 1", "5cm", "40", "10", "450", "4510", "04215", "Excel", "10/04/2022");
dt.Rows.Add("013", "Item 2", "6cm", "70", "80", "820", "5361", "0258", "Word", "04/05/2022");
dt.Rows.Add("014", "Item 3", "4cm", "210", "59", "890", "8468", "85642", "Text", "18/06/2022");
dt.Rows.Add("015", "Item 4", "9cm", "440", "70", "40", "8120", "47258", "Items", "20/07/2022");
dt.Rows.Add("016", "Item 5", "7cm", "450", "80", "50", "9812", "69140", "Excel", "31/08/2022");
Gvgridclient.DataSource = dt;
Gvgridclient.DataBind();
this.CalculateGrandTotal();
}
}
protected void quantity_TextChanged(object sender, EventArgs e)
{
double qt;
double qcost;
GridViewRow row = (sender as TextBox).NamingContainer as GridViewRow;
int kk = row.RowIndex;
qt = Convert.ToDouble(((TextBox)Gvgridclient.Rows[kk].FindControl("txtQuantity")).Text);
qcost = Convert.ToDouble(((TextBox)Gvgridclient.Rows[kk].FindControl("txtSalePrice")).Text);
((TextBox)Gvgridclient.Rows[kk].FindControl("txtsubtotal")).Text = (qt * qcost).ToString();
((Label)Gvgridclient.Rows[kk].FindControl("lblsubtotal")).Text = (qt * qcost).ToString();
this.CalculateGrandTotal();
if (kk< Gvgridclient.Rows.Count-1)
{
((TextBox)Gvgridclient.Rows[kk + 1].FindControl("txtItemCode")).Focus();
}
}
private void CalculateGrandTotal()
{
double total = 0;
foreach (GridViewRow row in Gvgridclient.Rows)
{
string subtotal = ((Label)row.FindControl("lblsubtotal")).Text;
total += !string.IsNullOrEmpty(subtotal) ? Convert.ToDouble(subtotal) : 0;
}
(Gvgridclient.FooterRow.FindControl("txtFsubtotal") as TextBox).Text = total.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(10) {New DataColumn("ItemCode"), New DataColumn("ItemDescription"), New DataColumn("ItemSize"), New DataColumn("Quantity"), New DataColumn("OrderPrice"), New DataColumn("SalePrice"), New DataColumn("SubTotal"), New DataColumn("Prno"), New DataColumn("Names"), New DataColumn("Date"), New DataColumn("DelSingle")})
dt.Rows.Add("012", "Item 1", "5cm", "40", "10", "450", "4510", "04215", "Excel", "10/04/2022")
dt.Rows.Add("013", "Item 2", "6cm", "70", "80", "820", "5361", "0258", "Word", "04/05/2022")
dt.Rows.Add("014", "Item 3", "4cm", "210", "59", "890", "8468", "85642", "Text", "18/06/2022")
dt.Rows.Add("015", "Item 4", "9cm", "440", "70", "40", "8120", "47258", "Items", "20/07/2022")
dt.Rows.Add("016", "Item 5", "7cm", "450", "80", "50", "9812", "69140", "Excel", "31/08/2022")
Gvgridclient.DataSource = dt
Gvgridclient.DataBind()
Me.CalculateGrandTotal()
End If
End Sub
Protected Sub quantity_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim qt As Double
Dim qcost As Double
Dim row As GridViewRow = TryCast((TryCast(sender, TextBox)).NamingContainer, GridViewRow)
Dim kk As Integer = row.RowIndex
qt = Convert.ToDouble((CType(Gvgridclient.Rows(kk).FindControl("txtQuantity"), TextBox)).Text)
qcost = Convert.ToDouble((CType(Gvgridclient.Rows(kk).FindControl("txtSalePrice"), TextBox)).Text)
CType(Gvgridclient.Rows(kk).FindControl("txtsubtotal"), TextBox).Text = (qt * qcost).ToString()
CType(Gvgridclient.Rows(kk).FindControl("lblsubtotal"), Label).Text = (qt * qcost).ToString()
Me.CalculateGrandTotal()
If kk < Gvgridclient.Rows.Count - 1 Then
CType(Gvgridclient.Rows(kk + 1).FindControl("txtItemCode"), TextBox).Focus()
End If
End Sub
Private Sub CalculateGrandTotal()
Dim total As Double = 0
For Each row As GridViewRow In Gvgridclient.Rows
Dim subtotal As String = (CType(row.FindControl("lblsubtotal"), Label)).Text
total += If(Not String.IsNullOrEmpty(subtotal), Convert.ToDouble(subtotal), 0)
Next
TryCast(Gvgridclient.FooterRow.FindControl("txtFsubtotal"), TextBox).Text = total.ToString()
End Sub
Screenshot