请大家帮忙!老出错,说Input string was not in a correct format

解决方案 »

  1.   

    HTML:<%@ Page language="c#" Codebehind="total.aspx.cs" AutoEventWireup="false" Inherits="myApp.total" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>total</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="total" method="post" runat="server">
    <FONT face="宋体"></FONT>
    <asp:datagrid id="grid" style="Z-INDEX: 101; LEFT: 57px; POSITION: absolute; TOP: 83px" runat="server"
    AutoGenerateColumns="False" ShowFooter="True" OnItemDataBound="MyDataGrid_ItemDataBound" FooterStyle-BackColor="Blue"
    FooterStyle-ForeColor="White" FooterStyle-Font-Bold="True" FooterStyle-Font-Size="10pt" FooterStyle-Font-Name="Verdana"
    HeaderStyle-BackColor="Blue" HeaderStyle-ForeColor="White" HeaderStyle-Font-Bold="True" HeaderStyle-Font-Size="10pt"
    HeaderStyle-Font-Name="Verdana" ItemStyle-Font-Size="9pt" ItemStyle-Font-Name="Verdana" Gridlines="None"
    BorderWidth="1" BorderStyle="Solid" BorderColor="Black" CellSpacing="0" CellPadding="4" Width="267px">
    <Columns>
    <asp:BoundColumn HeaderText="Title" DataField="title" />
    <asp:BoundColumn HeaderText="Price" DataField="price" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Center" />
    </Columns>
    </asp:datagrid>
    </form>
    </body>
    </HTML>CS代码:using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data;
    using System.Data.SqlClient;
    //using BDEServProj;namespace myApp
    {
    public class total : Page
    {
    protected DataGrid MyGrid;
    protected System.Web.UI.WebControls.DataGrid grid;
    private double runningTotal = 0;
           // BDEServProj.BDEServClass aa= new BDEServProj.BDEServClass();

    private void CalcTotal(string _price)
    {
    try
    {
    runningTotal += Double.Parse(_price);
    }
    catch
    {      
    //A value was null
    }
    }
     
    public void MyDataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
    CalcTotal( e.Item.Cells[1].Text );
    e.Item.Cells[1].Text = string.Format("{0:c}", Convert.ToDouble(e.Item.Cells[1].Text));
    }
    else if(e.Item.ItemType == ListItemType.Footer )
    {
    e.Item.Cells[0].Text="Total";
    e.Item.Cells[1].Text = string.Format("{0:c}", runningTotal);
    }  
    } private void InitializeComponent()
    {
    this.Load += new System.EventHandler(this.Page_Load); }
    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    if (!Page.IsPostBack)
    {

    SqlConnection cnn=new SqlConnection(@"Data Source=jerry;persist security info=False;user id=sa;password=;Initial Catalog=test");
    SqlDataAdapter da=new SqlDataAdapter("SELECT title, price FROM total WHERE 1=1", cnn);
    DataSet ds = new DataSet();

    da.Fill(ds, "total");

    this.DataGrid1.DataSource = ds;
    this.DataGrid1.DataMember = "total";
    this.DataGrid1.DataBind();
    }

    } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    } private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
    CalcTotal( e.Item.Cells[1].Text);
    e.Item.Cells[1].Text = string.Format("{0:c}", Convert.ToDouble(e.Item.Cells[1].Text));
    }
    else if(e.Item.ItemType == ListItemType.Footer )
    {
    e.Item.Cells[0].Text="Total";
    e.Item.Cells[1].Text = string.Format("{0:c}", runningTotal);
    }  
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary> #endregion
    }
    }
      

  2.   

    是不是你的格式不正确?
    http://eu.webmatrixhosting.net/sharp_c/forum/MessageInfo.aspx?MessageID=64