2013年9月12日星期四

Gridview cell text String turn float




 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //如果是绑定数据行 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
        //自动计算8.5单价X数量后的金额 
            if (e.Row.Cells[7].Text != null)
            {
                float Pbdiscount = Convert.ToSingle(e.Row.Cells[6].Text.Trim()); //8.5折后单价
                float Enum = Convert.ToSingle(e.Row.Cells[7].Text.Trim()); //数量
                String sumEprice =(Pbdiscount*Enum).ToString(); //金额
                e.Row.Cells[8].Text = sumEprice;
            }
         }
     }


I want to edit this line amounts of data , when the user enters , subsequent amount computed automatically.
But when prompted to debug this error . Is not my idea of ​​wrong, what the solution ?

------ Solution ------------------------------------ --------

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //如果是绑定数据行 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
        //自动计算8.5单价X数量后的金额 
            if (e.Row.Cells[7].Text != null)
            {
float Pbdiscount =0;
if (e.Row.Cells[6].Text.Trim()!="")
{
   Pbdiscount = Convert.ToSingle(e.Row.Cells[6].Text.Trim()); //8.5折后单价
}
                float Enum = Convert.ToSingle(e.Row.Cells[7].Text.Trim()); //数量
                String sumEprice =(Pbdiscount*Enum).ToString(); //金额
                e.Row.Cells[8].Text = sumEprice;
            }
         }
     }

------ Solution -------------------------- ------------------
refer:
float aa;
            bool flag = float.TryParse("", out aa);
            if (flag)
                Console.WriteLine("yes");
            else
                Console.WriteLine("no");

------ For reference only ----------------------------------- ----
float.Parse (e.Row.Cells [6]. Text.Trim ()); / / try
------ For reference only ----- ----------------------------------





Line 30:                     Pbdiscount = Convert.ToSingle(e.Row.Cells[6].Text.Trim()); //8.5折后单价
Line 31:                 }
Line 32:                 float Enum = Convert.ToSingle(e.Row.Cells[7].Text.Trim()); //数量
Line 33:                 String sumEprice = (Pbdiscount * Enum).ToString(); //金额
Line 34:                 e.Row.Cells[8].Text = sumEprice;


still the same error ...
------ For reference only --------------------------- ------------

useless I tried ...
------ For reference only ---------- -----------------------------



thank you for reminding phrase Tip : if (e.Row.Cells [6]. Text.Trim ()! = "")
behind me also add to the number of judgments if (e.Row.Cells [7]. Text.Trim ()! = "")
then succeeded. .


没有评论:

发表评论