Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Gridview文本更改事件_C#_Gridview - Fatal编程技术网

C# Gridview文本更改事件

C# Gridview文本更改事件,c#,gridview,C#,Gridview,我正在计算gridview价格字段值的总和,它是gridview的textboxchanged事件中的textbox。但当光标移到这一行时:total+=Convert.ToDecimal(mytextbox)获取异常:输入字符串的格式不正确。以下是我的gridviewtext更改事件代码: protected void txtPrice_OnTextChanged(object sender, System.EventArgs e) { decimal total = 0.0m;

我正在计算gridview价格字段值的总和,它是gridview的textboxchanged事件中的textbox。但当光标移到这一行时:
total+=Convert.ToDecimal(mytextbox)获取异常:输入字符串的格式不正确。以下是我的gridviewtext更改事件代码:

protected void txtPrice_OnTextChanged(object sender, System.EventArgs e)
{
    decimal total = 0.0m;
    foreach (GridViewRow gvr in GrdTabRow.Rows)

    {
        if (gvr.RowType == DataControlRowType.DataRow)
        {
            TextBox tb = gvr.FindControl("txtPrice") as TextBox;
            string mytextbox = tb.ToString();
            if (!mytextbox.Equals("") && mytextbox != null)
            {
                total +=Convert.ToDecimal(mytextbox);
            }
        }
        GrdTabRow.FooterRow.Cells[2].Text = total.ToString();
    }  
}
尝试:


我相信ToString()返回的对象表示形式在对象之间有所不同,并不总是表示对象的特定值。有时它只返回对象的完整限定名。文本成员返回文本框的值。ToString()应该是可选的

文本框中是否有空格、逗号或货币符号?Convert to decimal要求您尝试转换的字符串是double的有效表示形式。不,它没有任何saplce或currecny符号。它在GridViwea中的nomral文本框绝对正确。它工作正常:string mytextbox=tb.text.ToString()只有tb。文本也应该足够了。
string mytextbox = tb.Text.ToString();
total += Convert.ToDecimal(mytextbox);