Reputation: 9
In my gridview i am having sellprice,quantity,discount,netamount. the user enters the quantity and discount and the result should be displayed in the netamount column, i am trying with this code it is not working and the last line i am substracting the value it is giving error please help me to solve the problem..... I am writing this code in the Text changed event .....
foreach (GridViewRow row in GridView1.Rows)
{
TextBox txtSerAmt = (TextBox)row.FindControl("TxtServiceAmount");
TextBox txtQuantity = (TextBox)row.FindControl("txtQuantity");
TextBox txtdsAmount = (TextBox)row.FindControl("txtdsAmount");
TextBox TxtNetAmt = (TextBox)row.FindControl("TxtNetAmt");
if (txtSerAmt != null && txtQuantity != null && txtdsAmount != null)
{
TxtNetAmt.Text = (((
Convert.ToDouble(txtSerAmt.Text) * Convert.ToDouble(txtQuantity.Text))) * Convert.ToDouble(txtdsAmount.Text)/100).ToString();
}
txttotal.text = Convert.ToDouble(txtTotal.Text) - Convert.ToDouble(txtNetAmt.Text)
}
Upvotes: 0
Views: 1813
Reputation: 1736
in string to double conversion first you check that the getting value not for null. if null means to putting manually '0' are otherwise, it should rises error and anther one thing after subtracting to most to convert to string like "Masoomian" answer
TextBox TxtNetAmt = (TextBox)row.FindControl("TxtNetAmt");
txttotal.text = (Convert.ToDouble(txtTotal.Text) -
Convert.ToDouble(txtNetAmt.Text)).ToString();
and you should check all the FindControl, the control id is correct. and this calculation only do in Rowbound Event
Upvotes: 0
Reputation: 7218
Closely observe the last line: there are txttotal and txtTotal, as well as txtNetAmt is mentioned as TxtNetAmt in earlier line.
There are some spelling differences.
**txttotal**.text = Convert.ToDouble(**txtTotal**.Text) - Convert.ToDouble(**txtNetAmt**.Text)
Upvotes: 0
Reputation: 752
txttotal.text = (Convert.ToDouble(txtTotal.Text) - Convert.ToDouble(txtNetAmt.Text)).ToString()
The reason why you are getting the error is txttotal.text is a string type where your calculation returns a number type.So you need to convert to string which is performed by the ToString method
Upvotes: 1