本文介绍了在onrowdatabound事件gridview中计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 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(); } } 我尝试过: 写完这段代码之后,它不能在下一栏上进行计算,请帮我计算一下这个值,当我做计算然后显示空引用异常时,使用try catch隐藏计算代码。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 .推荐答案我认为您的代码无法在gridview中找到Textbox控件 更改以下行I think your code failing to locate Textbox controls inside gridviewChange below linesTextBox 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 positionTextBox 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");请检查此 <%@ Page Language =C#AutoEventWireup =trueCodeBehind =gridcalc.aspx.csInherits =WebApplication1.gridcalc%> <!DOCTYPE html> < html xmlns =http://www.w3.org/1999/xhtml> < head runat =server> < title>< / title> < / head> < body> < form id =form1runat =server>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 =Gridview1runat =serverEmptyDataText =数据不可用AutoGenerateColumns =falseOnRowDataBound =Gridview1_RowDataBound> < columns> < asp:TemplateField HeaderText =ID> < itemtemplate> < asp:TextBox ID =t xtIDrunat =serverText ='<%#Eval(ID)%>'/> < asp:TemplateField HeaderText =Quantity> < itemtemplate> < asp:TextBox ID =txtQuantityrunat =serverAutoPostBack = TrueText ='<%#Eval(Quantity)%>'OnTextChanged =txtQuantity_TextChanged/> < asp:TemplateField HeaderText =Rate> < itemtemplate> < asp:TextBox ID =txtRaterunat =serverAutoPostBack =TrueText ='<%#Eval(Rate)%>'OnTextChanged =txtRate_TextChanged/> < asp:TemplateField HeaderText =Total> < itemtemplate> < asp:TextBox ID =txtTotalrunat =serverText ='<%#Eval(Total)%>'/> <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> 代码文件: </form></body></html>Code file: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"; } } }} 这篇关于在onrowdatabound事件gridview中计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-13 17:48