Calculate ASP.Net GridView row Total and Grand total on Button Click using C# and VB.Net

Last Reply 11 days ago By pandeyism

Posted 11 days ago

Dear sir,

i have small problem kindly help me.

i tried below code but i could not get actual values in every row and the sub total colum. As per the below code i have get value in the total cell namely cell[6].Text. But while multiple rows i have get value in the cell[6].Text only second row price * quantity value in short in second row calculated value if 2500 i get first row also 2500.

Kindly rectify the error.

        protected void Button3_Click(object sender, EventArgs e)
        {            
            decimal grandTotal = 0;            
            foreach (GridViewRow row in GridView1.Rows)
            {
                decimal quantity = Convert.ToDecimal((row.FindControl("txtQuantity") as TextBox).Text);
                decimal price = Convert.ToDecimal(row.Cells[3].Text);
                int x = GridView1.Rows.Count;
                for (int i = 0; i < x; i++)
                {
                    decimal total = price * quantity;
                    GridView1.Rows[i].Cells[6].Text = total.ToString();
                    grandTotal += total;
                }
            }
        }

 

 

You are viewing reply posted by: pandeyism 11 days ago.
Posted 11 days ago

Hi rajeesh,

Refer below sample.

HTML

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowFooter="True">
    <Columns>
        <asp:BoundField DataField="itemname" HeaderText="Item Name" />
        <asp:BoundField DataField="price" HeaderText="Price" />
        <asp:TemplateField HeaderText="Quantity">
            <FooterTemplate>
                <asp:Label ID="Label44" runat="server" Text="Sub Total"></asp:Label>
            </FooterTemplate>
            <ItemTemplate>
                <asp:TextBox ID="txtQuantity" runat="server" Height="30px" Text='<%# Eval("obqty") %>'
                    Width="40px"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Total">
            <ItemTemplate>
                <asp:Label ID="lblTotal" runat="server" Text="0"></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="lblGrandTotal" runat="server" Text="0"></asp:Label>
            </FooterTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:Button Text="Calculate" runat="server" OnClick="Button3_Click" />

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        System.Data.DataTable dt = new System.Data.DataTable();
        dt.Columns.AddRange(new System.Data.DataColumn[] {
                    new System.Data.DataColumn("itemname", typeof(string)),
                    new System.Data.DataColumn("obqty", typeof(int)),
                    new System.Data.DataColumn("price", typeof(int))});
        dt.Rows.Add("Pen", 10, 100);
        dt.Rows.Add("Pencil", 20, 100);
        dt.Rows.Add("Eraser", 30, 100);
        dt.Rows.Add("Highlighter", 40, 100);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}

protected void Button3_Click(object sender, EventArgs e)
{
    decimal grandTotal = 0;
    foreach (GridViewRow row in GridView1.Rows)
    {
        decimal quantity = Convert.ToDecimal((row.FindControl("txtQuantity") as TextBox).Text);
        decimal price = Convert.ToDecimal(row.Cells[1].Text);
        decimal total = 0;
        if (row.RowType == DataControlRowType.DataRow)
        {
            total = price * quantity;
            (row.FindControl("lblTotal") as Label).Text = total.ToString();
        }
        grandTotal += total;
    }
    (GridView1.FooterRow.FindControl("lblGrandTotal") as Label).Text = grandTotal.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 System.Data.DataTable = New System.Data.DataTable()
        dt.Columns.AddRange(New System.Data.DataColumn() {New System.Data.DataColumn("itemname", GetType(String)), New System.Data.DataColumn("obqty", GetType(Integer)), New System.Data.DataColumn("price", GetType(Integer))})
        dt.Rows.Add("Pen", 10, 100)
        dt.Rows.Add("Pencil", 20, 100)
        dt.Rows.Add("Eraser", 30, 100)
        dt.Rows.Add("Highlighter", 40, 100)
        GridView1.DataSource = dt
        GridView1.DataBind()
    End If
End Sub

Protected Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim grandTotal As Decimal = 0
    For Each row As GridViewRow In GridView1.Rows
        Dim quantity As Decimal = Convert.ToDecimal((TryCast(row.FindControl("txtQuantity"), TextBox)).Text)
        Dim price As Decimal = Convert.ToDecimal(row.Cells(1).Text)
        Dim total As Decimal = 0
        If row.RowType = DataControlRowType.DataRow Then
            total = price * quantity
            TryCast(row.FindControl("lblTotal"), Label).Text = total.ToString()
        End If
        grandTotal += total
    Next
    TryCast(GridView1.FooterRow.FindControl("lblGrandTotal"), Label).Text = grandTotal.ToString()
End Sub

Screenshot