Reputation: 1
I am calculating the total of gridview price field values which is textbox in textboxchanged event of gridview. but while cursor is coming to this line: total +=Convert.ToDecimal(mytextbox);
getting the exception: Input string was not in a correct format. Here is my gridviewtext changed event code:
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();
}
}
Upvotes: 0
Views: 2991
Reputation: 113
try:
string mytextbox = tb.Text.ToString();
total += Convert.ToDecimal(mytextbox);
ToString() I believe returns the objects representation which differs between objects and doesn't always reprsent a specific value of an object. Sometimes it will simply return the full qualified name of the object.The Text member returns the value of the TextBox. The ToString() should be optional
Upvotes: 2