Click here to Skip to main content
15,911,762 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txtRate = (TextBox)e.Row.FindControl("txtRate");
TextBox txtQuantity = (TextBox)e.Row.FindControl("txtQuantity");
TextBox txtTotal = (TextBox)e.Row.FindControl("txtTotal");

int R = int.Parse(txtRate.Text);
int Q = int.Parse(txtQuantity.Text);
int T = int.Parse(txtTotal.Text);

T = Convert.ToInt32(R * Q);
txtTotal.Text = T.ToString();
}
}

What I have tried:

After written this code its cant be calculationg on next column, please help me to calculate the value, when im doing calculation then its showing nullreference exception, after using try catch its hiding the calculation code .
Posted
Updated 17-Nov-16 21:46pm

I think your code failing to locate Textbox controls inside gridview
Change below lines
XML
TextBox txtRate = (TextBox)e.Row.FindControl("txtRate");
TextBox txtQuantity = (TextBox)e.Row.FindControl("txtQuantity");
TextBox txtTotal = (TextBox)e.Row.FindControl("txtTotal");

use something like this, change column index according to your column position
XML
TextBox txtRate = (TextBox)e.Row.Cells[1].FindControl("txtRate");
TextBox txtQuantity = (TextBox)e.Row.Cells[2].FindControl("txtQuantity");
TextBox txtTotal = (TextBox)e.Row.Cells[3].FindControl("txtTotal");
 
Share this answer
 
Comments
kiran kamble 16-Nov-16 13:28pm    
i was tried like that also but its showing formatexception was not handled by user code and input string was not in correct format
manu_dhobale 17-Nov-16 13:25pm    
share code including gridview structure
kiran kamble 18-Nov-16 1:19am    
<asp:GridView ID="Gridview1" runat="server" EmptyDataText="Data is not available" OnRowDataBound="Gridview1_RowDataBound" AutoGenerateColumns="false">
<columns>
<asp:TemplateField HeaderText="ID">
<itemtemplate>
<asp:TextBox ID="txtID" runat="server" Text='<%#Eval("ID") %>' />


<asp:TemplateField HeaderText="Quantity">
<itemtemplate>
<asp:TextBox ID="txtQuantity" runat="server" Text='<%#Eval("Quantity") %>'/>


<asp:TemplateField HeaderText="Rate">
<itemtemplate>
<asp:TextBox ID="txtRate" runat="server" Text='<%#Eval("Rate") %>'/>


<asp:TemplateField HeaderText="Total">
<itemtemplate>
<asp:TextBox ID="txtTotal" runat="server" Text='<%#Eval("Total") %>'/>
<%--<asp:Label ID="Total" runat="server" Text = '<%#(Int32)Eval("Quantity") - (Int32)Eval("Rate")%>' />--%>







int R , Q , T = 0;
protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txtRate = (TextBox)e.Row.Cells[1].FindControl("txtRate");
TextBox txtQuantity = (TextBox)e.Row.Cells[2].FindControl("txtQuantity");
TextBox txtTotal = (TextBox)e.Row.Cells[3].FindControl("txtTotal");

R = int.Parse(txtRate.Text);
Q = int.Parse(txtQuantity.Text);

T += Convert.ToInt32(R * Q);
txtTotal.Text = T.ToString();
}
}
Please check this
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="gridcalc.aspx.cs" Inherits="WebApplication1.gridcalc" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">

<asp:GridView ID="Gridview1" runat="server" EmptyDataText="Data is not available" AutoGenerateColumns="false" OnRowDataBound="Gridview1_RowDataBound">
<columns>
<asp:TemplateField HeaderText="ID">
<itemtemplate>
<asp:TextBox ID="txtID" runat="server" Text='<%#Eval("ID") %>' />


<asp:TemplateField HeaderText="Quantity">
<itemtemplate>
<asp:TextBox ID="txtQuantity" runat="server" AutoPostBack="True" Text='<%#Eval("Quantity") %>' OnTextChanged="txtQuantity_TextChanged" />


<asp:TemplateField HeaderText="Rate">
<itemtemplate>
<asp:TextBox ID="txtRate" runat="server" AutoPostBack="True" Text='<%#Eval("Rate") %>' OnTextChanged="txtRate_TextChanged" />


<asp:TemplateField HeaderText="Total">
<itemtemplate>
<asp:TextBox ID="txtTotal" runat="server" Text='<%#Eval("Total") %>' />





</form>
</body>
</html>

Code file:
C#
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class gridcalc : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                DataTable dtPart = new DataTable();
                dtPart.Columns.Add("ID", typeof(int));
                dtPart.Columns.Add("Quantity", typeof(int));
                dtPart.Columns.Add("Rate", typeof(int));
                dtPart.Columns.Add("Total", typeof(int));


                dtPart.Rows.Add(25, 1, 2, 3);
                dtPart.Rows.Add(50, 4, 5, 6);

                Gridview1.DataSource = dtPart;
                Gridview1.DataBind();

            }
        }

        protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TextBox txtRate = (TextBox)e.Row.Cells[1].FindControl("txtRate");
                TextBox txtQuantity = (TextBox)e.Row.Cells[2].FindControl("txtQuantity");
                TextBox txtTotal = (TextBox)e.Row.Cells[3].FindControl("txtTotal");
                int T = 0;
                var R = int.Parse(txtRate.Text);
                var Q = int.Parse(txtQuantity.Text);

                T += Convert.ToInt32(R * Q);
                txtTotal.Text = T.ToString();
            }
        }

        protected void txtQuantity_TextChanged(object sender, EventArgs e)
        {
            TextBox qtxt = (TextBox)sender;
            GridViewRow gvRow = (GridViewRow)qtxt.Parent.Parent;
            TextBox Rate = (TextBox)gvRow.FindControl("txtRate");
            TextBox Total = (TextBox)gvRow.FindControl("txtTotal");
            try
            {
                Total.Text = ((Convert.ToInt32(qtxt.Text)) * (Convert.ToInt32(Rate.Text))).ToString();
            }
            catch { Total.Text = "0"; qtxt.Text = "0"; }
        }

        protected void txtRate_TextChanged(object sender, EventArgs e)
        {
            TextBox Rate = (TextBox)sender;
            GridViewRow gvRow = (GridViewRow)Rate.Parent.Parent;
            TextBox qtxt = (TextBox)gvRow.FindControl("txtQuantity");
            TextBox Total = (TextBox)gvRow.FindControl("txtTotal");
            try
            {
                Total.Text = ((Convert.ToInt32(qtxt.Text)) * (Convert.ToInt32(Rate.Text))).ToString();
            }
            catch { Total.Text = "0"; qtxt.Text = "0"; }
        }
    }
}
 
Share this answer
 
v2

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