我有这个简单的计算,但由于某种原因,我没有找到问题所在。我得到"格式化输入字符串不正确"错误。
这是代码:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem dataItem = e.Item as GridDataItem;
if (dataItem["AlternCost"].Text !=null)
{
dataItem["TotCost"].Text = ((Convert.ToInt32(dataItem["AlternCost"].Text)) * (Convert.ToDouble(dataItem["Quantity"].Text))).ToString();
}
else
{
dataItem["TotCost"].Text = ((Convert.ToInt32(dataItem["Cost"].Text)) * (Convert.ToDouble(dataItem["Quantity"].Text))).ToString();
}
}
}
错误在这一行:
dataItem["TotCost"].Text =
((Convert.ToInt32(dataItem["AlternCost"].Text)) *
(Convert.ToDouble(dataItem["Quantity"].Text))).ToString();
这是我的堆栈跟踪:
System.Number.StringToNumber中的(String str,NumberStyles选项, NumberBuffer&安培; number,NumberFormatInfo info,Boolean parseDecimal)
在System.Number.ParseInt32(String s,NumberStyles样式, System.Convert.ToInt32(String value)中的NumberFormatInfo info) in xxx_621.RadGrid1_ItemDataBound(Object sender,GridItemEventArgs e) 在c:\ Users \ xxx \ Documents \ Visual Studio中 2012 \ Projects \ ^^ xxxxxxxxxxx - BOM \ members \ xxx_621.aspx.cs:riga 57 in Telerik.Web.UI.RadGrid.OnItemDataBound(GridItemEventArgs e)in Telerik.Web.UI.RadGrid.CallOnItemDataBound(GridItemEventArgs e)in Telerik.Web.UI.GridItem.SetupItem(Boolean dataBind,Object dataItem, GridColumn []列,ControlCollection行) Telerik.Web.UI.GridItemBuilder.InitializeItem(Int32 dataSourceIndex, 字符串和放大器; nextItemHierarchicalIndex,Boolean& itemIsInEditMode)in Telerik.Web.UI.GridItemBuilder.CreateItems(GridGroupingContext group) 在Telerik.Web.UI.GridTableView.CreateItems(IEnumerator枚举器, GridColumn []列,ControlCollection控件)中 Telerik.Web.UI.GridTableView.CreateControlHierarchy(布尔 useDataSource)in Telerik.Web.UI.GridTableView.CreateChildControls(IEnumerable的 dataSource,Boolean useDataSource)in System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable的 数据) System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable的 数据) System.Web.UI.DataSourceView.Select(DataSourceSelectArguments 参数,DataSourceViewSelectCallback回调) System.Web.UI.WebControls.DataBoundControl.PerformSelect()in Telerik.Web.UI.GridTableView.PerformSelect()in System.Web.UI.WebControls.BaseDataBoundControl.DataBind()in Telerik.Web.UI.GridTableView.DataBind()in Telerik.Web.UI.RadGrid.DataBind()in System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound()in System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() 在System.Web.UI.Control.EnsureChildControls()中 System.Web.UI.Control.FindControl(String id,Int32 pathOffset)in System.Web.UI.Control.FindControl(String id)in Telerik.Web.ChildControlHelper.FindControlRecursive(String ID,Control Telerik.Web.ChildControlHelper.FindControlRecursive(String。) ID,控制根) Telerik.Web.ChildControlHelper.FindControlRecursive(String ID,Control 根) Telerik.Web.ChildControlHelper.FindControlRecursive(控制搜索者, 字符串ID) Telerik.Web.UI.RadAjaxControl.FindControlRecursive(String ID)in Telerik.Web.UI.RadAjaxControl.OnPagePreRender(Object sender,EventArgs e)在System.EventHandler.Invoke(Object sender,EventArgs e)中 System.Web.UI.Control.OnPreRender(EventArgs e)in System.Web.UI.Control.PreRenderRecursiveInternal()in System.Web.UI.Page.ProcessRequestMain(布尔 includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)
有人可以给我一个如何解决问题的提示吗?
答案 0 :(得分:1)
不应该是:
dataItem["TotCost"].Text = ((Convert.ToDouble(dataItem["AlternCost"].Text)) * (Convert.ToDouble(dataItem["Quantity"].Text))).ToString();
而不是:
dataItem["TotCost"].Text = ((Convert.ToInt32(dataItem["AlternCost"].Text)) * (Convert.ToDouble(dataItem["Quantity"].Text))).ToString();
请注意两种情况下的Convert.ToDouble而不是ToInt32和ToDouble?