输入字符串格式不正确

本文关键字:不正确 格式 字符串 输入 | 更新日期: 2023-09-27 18:09:39

我有这个简单的计算,但由于某种原因,我没有捡到问题所在。我得到"格式输入字符串不正确"错误。

这是代码:

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

。stringtonnumbernumber(字符串str, NumberStyles选项)NumberBuffer&number, NumberFormatInfo info, Boolean, parseDecimal)
在System.Number。ParseInt32(字符串s, NumberStyles的样式)NumberFormatInfo info)在System.Convert。ToInt32(字符串值)
在xxx_621。RadGrid1_ItemDataBound(对象发送者,GridItemEventArgs e)c:'Users'xxx'Documents'Visual Studio2012 ' '项目^ ^ xxxxxxxxxxx -BOM'members'xxx_621.aspx.cs:riga 57 inTelerik.Web.UI.RadGrid。OnItemDataBound(GridItemEventArgs) inTelerik.Web.UI.RadGrid。CallOnItemDataBound(GridItemEventArgs) inTelerik.Web.UI.GridItem。SetupItem(布尔数据绑定,对象数据项)GridColumn[]列,ControlCollection行)Telerik.Web.UI.GridItemBuilder。InitializeItem (Int32 dataSourceIndex,String&nextItemHierarchicalIndex Boolean&itemIsInEditMode)Telerik.Web.UI.GridItemBuilder。createitem (GridGroupingContext组)在Telerik.Web.UI.GridTableView。createitem (IEnumerator枚举器,GridColumn[] columns, ControlCollection控件)Telerik.Web.UI.GridTableView.CreateControlHierarchy(布尔useDataSource)Telerik.Web.UI.GridTableView.CreateChildControls (IEnumerabledataSource,布尔值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 ()Telerik.Web.UI.GridTableView.PerformSelect ()System.Web.UI.WebControls.BaseDataBoundControl.DataBind ()Telerik.Web.UI.GridTableView.DataBind ()Telerik.Web.UI.RadGrid.DataBind ()System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound ()System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls ()ensurechildcontrols (System.Web.UI.Control。FindControl(String id, Int32 pathffset) inSystem.Web.UI.Control。FindControl(字符串id) inTelerik.Web.ChildControlHelper。FindControlRecursive(字符串ID在Telerik.Web.ChildControlHelper.FindControlRecursive(字符串ID, Control root) inTelerik.Web.ChildControlHelper。FindControlRecursive(字符串ID根)Telerik.Web.ChildControlHelper。FindControlRecursive(控制搜索者,字符串ID)Telerik.Web.UI.RadAjaxControl。FindControlRecursive(字符串ID) inTelerik.Web.UI.RadAjaxControl。OnPagePreRender(对象发送者,EventArgse)在System.EventHandler中。调用(对象发送方,EventArgs e) inSystem.Web.UI.Control。OnPreRender(EventArgs e) inSystem.Web.UI.Control.PreRenderRecursiveInternal ()System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

谁能给我点提示如何解决这个问题?

输入字符串格式不正确

应该是:

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();

注意转换。ToDouble在这两种情况下,而不是ToInt32和ToDouble?