Link to home
Start Free TrialLog in
Avatar of Budrophious
BudrophiousFlag for United States of America

asked on

How do I compare cell value after using DataFormatString to format percentage?

I would like to use DataFormatString="{0:p}" on a column in my GridView to format a decimal to a percentage.  Prior to using DataFormatString, I was able to color the background of the cell depending on the value of the cell with the GridView and Code Behind shown below.  The sections commented out in the Code Behind are my attempts to compare the value and color the cell after the cell has been converted to a percentage using DataFormatString.  How do I compare the value of the cell after I use DataFormatString?  Thank you in advance!
<asp:GridView ID="my_GridView" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" onrowdatabound="my_GridView_RowDataBound">
    <Columns>
        <asp:BoundField DataField="my_date" HeaderText="Date" ReadOnly="True" SortExpression="date" />
        <%-- <asp:BoundField DataField="my_rate" HeaderText="Rate" ReadOnly="True" SortExpression="my_rate"/> --%>
        <asp:BoundField DataField="my_rate" HeaderText="Rate" ReadOnly="True" SortExpression="my_rate" DataFormatString="{0:p}"/> 
    </Columns>
</asp:GridView>
 
 
protected void my_GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow) 
    {
        float cellvalue;
        bool b = float.TryParse(e.Row.Cells[1].Text, out cellvalue);
        if (b)
        b = float.TryParse(e.Row.Cells[1].Text, out cellvalue);
        {
            if (cellvalue >= 0.90)
            {
                e.Row.Cells[1].BackColor = System.Drawing.ColorTranslator.FromHtml("#33FF00");  //Green
            }
                
            if ((cellvalue < 0.90) && (cellvalue >= 0.80))
            {
                e.Row.Cells[1].BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFF00");  //Yellow
            }
            if (cellvalue < 0.80)
            {
                e.Row.Cells[1].BackColor = System.Drawing.ColorTranslator.FromHtml("#FF0000");  //Red
            }
 
            //double value = double.Parse(cellvalue.Replace(" %", string.Empty)) / 100;
            //double value=double.Parse(cellvalue.Replace("%","").Trim()))/100;
            /*
            if (cellvalue == 0)
            {
                e.Row.Cells[1].BackColor = System.Drawing.ColorTranslator.FromHtml("#FF0000");  //Red
            }
            */
 
            /*
            double.Parse(str.Replace(" %", string.Empty)) / 100
            double value=double.Parse(percentString.Replace("%","").Trim()))/100;
            */
        }
    }
}

Open in new window

Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

Did u try to place a breakpoint and check what value do u get in


cellvalue : Line 19
Avatar of Budrophious

ASKER

I have not.  Which line are you referring to?
           if (cellvalue >= 0.90)
I made a correction (I was assigning to b twice).  

cellvalue is 0.0.  

The if statement is not being entered.
protected void my_GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow) 
    {
        float cellvalue;
        bool b = float.TryParse(e.Row.Cells[1].Text, out cellvalue);
        if (b)
        {
            if (cellvalue >= 0.90)
            {
                e.Row.Cells[1].BackColor = System.Drawing.ColorTranslator.FromHtml("#33FF00");  //Green
            }
                
            if ((cellvalue < 0.90) && (cellvalue >= 0.80))
            {
                e.Row.Cells[1].BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFF00");  //Yellow
            }
            if (cellvalue < 0.80)
            {
                e.Row.Cells[1].BackColor = System.Drawing.ColorTranslator.FromHtml("#FF0000");  //Red
            }
 
            //double value = double.Parse(cellvalue.Replace(" %", string.Empty)) / 100;
            //double value=double.Parse(cellvalue.Replace("%","").Trim()))/100;
            /*
            if (cellvalue == 0)
            {
                e.Row.Cells[1].BackColor = System.Drawing.ColorTranslator.FromHtml("#FF0000");  //Red
            }
            */
 
            /*
            double.Parse(str.Replace(" %", string.Empty)) / 100
            double value=double.Parse(percentString.Replace("%","").Trim()))/100;
            */
        }
    }
}

Open in new window

What is actually hapenning I feel is....

float.TryParse(e.Row.Cells[1].Text, out cellvalue);


e.Row.Cells[1].Text is returning non numeric.. Can u confirm that...
The value of e.Row.Cells[1].Text is "90.37 %".
ASKER CERTIFIED SOLUTION
Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India 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
Yes, I am able to use that and compare the cellvalue to the decimal value (without it being divided by 100).

So I am comparing cellvalue to 90 instead of 0.90.

Where would I divide it by 100 if I want to compare to 0.90?
protected void my_GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow) 
    {
        float cellvalue;
        bool b = float.TryParse(e.Row.Cells[3].Text.Split('%')[0].Trim(), out cellvalue);
        if (b)
        {
            if (cellvalue >= 90.00f)
            {
                e.Row.Cells[1].BackColor = System.Drawing.ColorTranslator.FromHtml("#33FF00");  //Green
            }
                
            if ((cellvalue < 90.00f) && (cellvalue >= 80.00f))
            {
                e.Row.Cells[1].BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFF00");  //Yellow
            }
            if (cellvalue < 80.00f)
            {
                e.Row.Cells[1].BackColor = System.Drawing.ColorTranslator.FromHtml("#FF0000");  //Red
            }
        }
    }
}

Open in new window

SOLUTION
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
That will work.  Thank you!