Link to home
Start Free TrialLog in
Avatar of vicomin
vicominFlag for United States of America

asked on

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

Avatar of Edgard Yamashita
Edgard Yamashita
Flag of Brazil image

try to convert to decimal first eg:

int iLabor = Convert.ToInt32(Convert.ToDecimal(sLabor));
ASKER CERTIFIED SOLUTION
Avatar of Edgard Yamashita
Edgard Yamashita
Flag of Brazil image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vicomin

ASKER

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

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;
    }
Avatar of vicomin

ASKER

string sValue = System.Math.Round(Decimal.Divide(iValue, 60)).ToString();