calculate value for gridview label

i need to calculate the value and put in a gridview label. see the code.

error: Input string was not in a correct format.
protected void grdvw_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView drv = (DataRowView)e.Row.DataItem;

if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblLabor = (Label)e.Row.FindControl("lblLabor");
string MainString = drv["reserved_isva"].ToString().Remove(0, 34);
string SearchString = " ";
int FirstChr = MainString.IndexOf(SearchString);
string cleanLabor = MainString.Remove(FirstChr);
lblLabor.Text = String.Format("{0:C2}", cleanLabor); //175.00

Label lblManHours = (Label)e.Row.FindControl("lblManHours");
lblManHours.Text = getMH(lblLabor.Text); //"175.00"
}


public static string getMH(string sLabor) //"175.00"
    {
        int iLabor = Convert.ToInt32(sLabor); //errors here
        int iValue = iLabor / 60;
        string sValue = Convert.ToString(iValue); //should equal "2.91666"
        return sValue;
    }

Open in new window

vicominAsked:
Who is Participating?
 
eguilhermeCommented:
oh also in here:

        int iValue = iLabor / 60;
        string sValue = Convert.ToString(iValue); //should equal "2.91666"
        return sValue;


it will not equal to 2.91666 since your dividing and setting the value in a int property not a decimal one maybe you should try like this:


        string sValue = Decimal.Divide(iLabor, 60).ToString();
        return sValue;
0
 
eguilhermeCommented:
try to convert to decimal first eg:

int iLabor = Convert.ToInt32(Convert.ToDecimal(sLabor));
0
 
vicominAuthor Commented:
slight modification
public static string getMH(string sLabor, string sQty) //"175.00", "2"
    {
        int iLabor = Convert.ToInt32(Convert.ToDecimal(sLabor));
        int iQty = Convert.ToInt32(sQty);
        int iValue = (iLabor * iQty);
        string sValue = Decimal.Divide(iValue, 60).ToString();//5.8333333333333333333333333333
        //round up so the above would be 6
        return sValue;
    }

Open in new window

0
 
princeatapiCommented:
may be you could use this function instead
public static string getMH(string sLabor) //"175.00"
    {
        double  iLabor = Convert.ToDouble(sLabor); //errors here
        double iValue = iLabor / 60.0;
        string sValue = Convert.ToString(iValue); //should equal "2.91666"
        return sValue;
    }
0
 
vicominAuthor Commented:
string sValue = System.Math.Round(Decimal.Divide(iValue, 60)).ToString();
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.