我正面临输入stringn不正确格式的异常 [英] i am facing exception that is input stringn was not in correct formate

查看:46
本文介绍了我正面临输入stringn不正确格式的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击更新按钮然后我插入新行,所以它给出数量异常

我的代码如下:



< pre lang =cs> private void dgDetail_CellEndEdit( object sender,DataGridViewCellEventArgs e)
{



if (dgDetail.CurrentRow! = null
{
if (dgDetail.CurrentRow.Cells [ 数量]。值!= null & ;& dgDetail.CurrentRow.Cells [ Rate]。值!= null
{
if (dgDetail.CurrentRow.Cells [ 数量]。值!= null & ;&安培; dgDetail.CurrentRow.Cells [ 数量]。Value.ToString()。Trim()!= && Convert.ToDecimal(dgDetail.CurrentRow.Cells [ 数量]。值)> 0
{
decimal GAmount = Convert.ToDecimal(dgDetail.CurrentRow.Cells [ Rate]。Value.ToString( ))* Convert.ToDecimal(dgDetail.CurrentRow.Cells [ Qty]。Value.ToString( ));
dgDetail.CurrentRow.Cells [ 金额]。Value = Convert.ToDecimal(GAmount) );

if (dgDetail.CurrentRow.Cells [ STPercent]。值!= null
{
dgDetail.CurrentRow.Cells [ SaleTax]。值= GAmount / 100 * Convert.ToDecimal(dgDetail.CurrentRow.Cells [ STPercent]。Value.ToString());
dgDetail.CurrentRow.Cells [ Total]。Value = GAmount + Convert.ToDecimal (dgDetail.CurrentRow.Cells [ SaleTax]。Value.ToString());


}


}
}



}





}



























例外情况下列行





十进制GAmount = Convert.ToDecimal(dgDetail.CurrentRow.Cells [Rate]。Value.ToString())* Convert.ToDecimal(dgDetail.CurrentRow.Cells [Qty ] .Value.ToString());

解决方案

您需要在该行上放置一个断点并查看单元格包含的内容。

首先将线分成单独的字符串可能会更容易:

  string  s1 = dgDetail.CurrentRow .Cells [  Rate]。Value.ToString(); 
string s2 = dgDetail.CurrentRow.Cells [ 数量] Value.ToString();
decimal GAmount = Convert.ToDecimal(s1)* Convert.ToDecimal(s2);





不幸的是,这是你的数据,而不是我们的数据,所以我们可以看看它,甚至开始告诉你问题是什么。调试器应该帮助你找出你是否看到上面的s1和s2的值。


HI


试试这样,< br $> b $ b

 十进制 GAmount,Rate,Qty; 
decimal .TryParse(dgDetail.CurrentRow.Cells [ Rate]。值+ out 率);
decimal .TryParse(dgDetail.CurrentRow.Cells [ 数量]。值+ out 数量);
GAmount = Rate * Qty;


Tyr this。



  decimal  GAmount = Convert.ToDecimal( string  .IsNullOrEmpty(dgDetail.CurrentRow.Cells [   Rate]。Value)==  true  0 :dgDetail.CurrentRow.Cells [ 评价]。价值)
* Convert.ToDecimal( string .IsNullOrEmpty(dgDetail.CurrentRow.Cells [ 数量]。值)== true 0 ,dgDetail.CurrentRow.Cells [ 数量] 。值);


when i click on update button then i insert new row so it gives exception on quantity
my code is given below

private void dgDetail_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {



             if (dgDetail.CurrentRow != null)
            {
                if (dgDetail.CurrentRow.Cells["Qty"].Value != null && dgDetail.CurrentRow.Cells["Rate"].Value != null)
                {
                    if (dgDetail.CurrentRow.Cells["Qty"].Value != null && dgDetail.CurrentRow.Cells["Qty"].Value.ToString().Trim() != "" && Convert.ToDecimal(dgDetail.CurrentRow.Cells["Qty"].Value) > 0)
                  {
                        decimal GAmount = Convert.ToDecimal(dgDetail.CurrentRow.Cells["Rate"].Value.ToString()) * Convert.ToDecimal(dgDetail.CurrentRow.Cells["Qty"].Value.ToString());
                        dgDetail.CurrentRow.Cells["Amount"].Value = Convert.ToDecimal(GAmount);

                    if (dgDetail.CurrentRow.Cells["STPercent"].Value != null)
                    {
                        dgDetail.CurrentRow.Cells["SaleTax"].Value = GAmount / 100 * Convert.ToDecimal(dgDetail.CurrentRow.Cells["STPercent"].Value.ToString());
                        dgDetail.CurrentRow.Cells["Total"].Value = GAmount + Convert.ToDecimal(dgDetail.CurrentRow.Cells["SaleTax"].Value.ToString());


                    }


                    }
                }



            }





            }














exception accuring following lines


decimal GAmount = Convert.ToDecimal(dgDetail.CurrentRow.Cells["Rate"].Value.ToString()) * Convert.ToDecimal(dgDetail.CurrentRow.Cells["Qty"].Value.ToString());

解决方案

You need to put a breakpoint on the line and look at what the cells contain.
Breaking the line out into separate strings first might make that easier:

string s1 = dgDetail.CurrentRow.Cells["Rate"].Value.ToString();
string s2 = dgDetail.CurrentRow.Cells["Qty"].Value.ToString();
decimal GAmount = Convert.ToDecimal(s1) * Convert.ToDecimal(s2);



Unfortunately, it's your data, not ours, so we can;t look at it and even begin to tell you what the problems is. The debugger should help you to find out though if you look at the values of s1 and s2 above.


HI
Try like this,

decimal GAmount, Rate, Qty;
            decimal.TryParse(dgDetail.CurrentRow.Cells["Rate"].Value + "", out Rate);
            decimal.TryParse(dgDetail.CurrentRow.Cells["Qty"].Value + "", out Qty);
            GAmount = Rate * Qty;


Tyr this.

decimal GAmount = Convert.ToDecimal(string.IsNullOrEmpty(dgDetail.CurrentRow.Cells["Rate"].Value) == true ? 0 : dgDetail.CurrentRow.Cells["Rate"].Value)
                                     * Convert.ToDecimal(string.IsNullOrEmpty(dgDetail.CurrentRow.Cells["Qty"].Value)==true ? 0,dgDetail.CurrentRow.Cells["Qty"].Value);


这篇关于我正面临输入stringn不正确格式的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆