Tuesday 28 February 2012

calculate a gridview cell values

If you want to calculate gridview cell value, you will use the foreach method .


 decimal decTotal = 0;
                foreach (GridViewRow grd in grdDetail.Rows) // grdDetail -->gridview Id
                {
                    TextBox t1 = (TextBox)grd.FindControl("txtItem");  //find the grid textboxvalue
                    TextBox t2 = (TextBox)grd.FindControl("txtQuantity");
                    TextBox t3 = (TextBox)grd.FindControl("txtPrice");
                    Label lbl = (Label)grd.FindControl("lblTotal");
                    if (t1.Text != "" || t2.Text != "" || t3.Text != "")
                    {
                        lbl.Text = Convert.ToString(Convert.ToDecimal(t2.Text) * Convert.ToDecimal(t3.Text));
                        decTotal = decTotal + Convert.ToDecimal(lbl.Text);  //add cell values
                     }
                   
               }
                lblTotal.Text = decTotal.ToString();  //store total in a label

use the above specified method . you will calculate the gridview cell values.

No comments:

Post a Comment