问题描述
求GridView控件求第五列的和,下面这样写对不对啊code=csharp]protectedvoidGVData_RowDataBound(objectsender,GridViewRowEventArgse){intsum=0;for(inti=0;i<GVData.Rows.Count;i++){if(GVData.Rows[i].Cells[4].Text.Trim()!=""){sum=sum+Convert.ToInt32(GVData.Rows[i].Cells[4].Text.Trim());}else{continue;}}Response.Write("<script>alert('"+sum+"')</script>");[/code]运行以后出错怎么搞啊“/web”应用程序中的服务器错误。输入字符串的格式不正确。说明:执行当前Web请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。异常详细信息:System.FormatException:输入字符串的格式不正确。源错误:行219:if(GVData.Rows[i].Cells[4].Text.Trim()!="")行220:{行221:sum=sum+Convert.ToInt32(GVData.Rows[i].Cells[4].Text.Trim());行222:行223:}源文件:c:UsersAdministratorDesktop2015.4.29webyijianDocCenterF.aspx.cs行:221堆栈跟踪:[FormatException:输入字符串的格式不正确。]System.Number.StringToNumber(Stringstr,NumberStylesoptions,NumberBuffer&number,NumberFormatInfoinfo,BooleanparseDecimal)+7475735System.Number.ParseInt32(Strings,NumberStylesstyle,NumberFormatInfoinfo)+119System.Convert.ToInt32(Stringvalue)+63DocCenter_DocCenter.GVData_RowDataBound(Objectsender,GridViewRowEventArgse)inc:UsersAdministratorDesktop2015.4.29webyijianDocCenterF.aspx.cs:221System.Web.UI.WebControls.GridView.OnRowDataBound(GridViewRowEventArgse)+108System.Web.UI.WebControls.GridView.CreateRow(Int32rowIndex,Int32dataSourceIndex,DataControlRowTyperowType,DataControlRowStaterowState,BooleandataBind,ObjectdataItem,DataControlField[]fields,TableRowCollectionrows,PagedDataSourcepagedDataSource)+167System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerabledataSource,BooleandataBinding)+2417System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerabledata)+57System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerabledata)+14System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerabledata)+114System.Web.UI.DataSourceView.Select(DataSourceSelectArgumentsarguments,DataSourceViewSelectCallbackcallback)+31System.Web.UI.WebControls.DataBoundControl.PerformSelect()+142System.Web.UI.WebControls.BaseDataBoundControl.DataBind()+73System.Web.UI.WebControls.GridView.DataBind()+4DocCenter_DocCenter.DataBindToGridview()inc:UsersAdministratorDesktop2015.4.29webyijianDocCenterF.aspx.cs:103DocCenter_DocCenter.Page_Load(Objectsender,EventArgse)inc:UsersAdministratorDesktop2015.4.29webyijianDocCenterF.aspx.cs:59System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtrfp,Objecto,Objectt,EventArgse)+14System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Objectsender,EventArgse)+35System.Web.UI.Control.OnLoad(EventArgse)+99System.Web.UI.Control.LoadRecursive()+50System.Web.UI.Page.ProcessRequestMain(BooleanincludeStagesBeforeAsyncPoint,BooleanincludeStagesAfterAsyncPoint)+627
解决方案
解决方案二:
protectedvoidGridView1_RowDataBound(objectsender,GridViewRowEventArgse){try{if(e.Row.RowIndex>=0){try{doublext=Convert.ToDouble(e.Row.Cells[4].Text.Replace("$",""))*Convert.ToDouble(e.Row.Cells[5].Text.Replace("$",""));totalprices+=xt;}catch{}}lb_prices.Text="<b>总金额:</b>"+totalprices.ToString("N");}catch{}}
解决方案三:
解决方案四:
有空字符把!
解决方案五:
用这个就不会报错了decimalnum=0;decimal.tryparse(字符串,outnum)