Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.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#_Asp.net_Valueconverter - Fatal编程技术网

C# 在gridview中选中复选框时计算金额

C# 在gridview中选中复选框时计算金额,c#,asp.net,valueconverter,C#,Asp.net,Valueconverter,选中该复选框并在标签中合计金额。由于从字符串转换为十进制时出现错误,我如何实现它。此处需要解决的两个问题: moviePrice变量的类型为Label,因此无法将其转换为十进制。你应该改用moviePrice.Text 当你计算总数时,它应该是total=total+abc 编辑:需要在循环外部声明Total变量。现在发生的是,您在循环中声明了变量,因此它在循环的每次迭代中都会被重置。我在检查第二个变量时仍然无法添加。Izzit需要使用循环。更新了我的答案。如果有帮助,请告诉我。 protect

选中该复选框并在标签中合计金额。由于从字符串转换为十进制时出现错误,我如何实现它。

此处需要解决的两个问题:

  • moviePrice变量的类型为Label,因此无法将其转换为十进制。你应该改用moviePrice.Text
  • 当你计算总数时,它应该是total=total+abc

  • 编辑:需要在循环外部声明Total变量。现在发生的是,您在循环中声明了变量,因此它在循环的每次迭代中都会被重置。

    我在检查第二个变量时仍然无法添加。Izzit需要使用循环。更新了我的答案。如果有帮助,请告诉我。
    protected void ChkPayment_CheckChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow gvrow in grvPaymentList.Rows)
        {
            var Selection = gvrow.FindControl("ChkSelected") as CheckBox;
    
            decimal Total=0;
            decimal abc=0;
            if (Selection.Checked)
            {
                var  moviePrice = gvrow.FindControl("MoviePrice") as Label ;
                abc = Convert.ToDecimal(moviePrice.Text);
            }
            Total = Total + abc;
            lblAmount.Text = Total.ToString();
        }        
    }