Calculate the amount while check the checkbox in gridview

903 views Asked by At
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();
    }        
}

Check The CheckBox and total the amount in label. How can I achieved it due to I am getting error for convert from string to decimal.

1

There are 1 answers

1
shree.pat18 On BEST ANSWER

2 things you need to fix here:

  1. The moviePrice variable is of type Label, hence you cannot convert it to Decimal. You should use moviePrice.Text instead.
  2. When you calculate the total, it should be Total = Total + abc.

EDIT: The Total variable needs to be declared outside the loop. What happens now is that you declare the variable inside the loop, so it gets reset during each iteration of the loop.