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

Budrophious
Budrophious used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Obadiah ChristopherDeveloper User Interface

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


cellvalue : Line 19

Author

Commented:
I have not.  Which line are you referring to?
Obadiah ChristopherDeveloper User Interface

Commented:
           if (cellvalue >= 0.90)
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Author

Commented:
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

Obadiah ChristopherDeveloper User Interface

Commented:
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...

Author

Commented:
The value of e.Row.Cells[1].Text is "90.37 %".
Developer User Interface
Commented:
Yeah so if u will try float.Parse on this value it will always return 0.0

Try something like this...

bool b = float.TryParse(e.Row.Cells[1].Text.Split('%')[0].Trim(), out cellvalue);

Author

Commented:
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

Obadiah ChristopherDeveloper User Interface
Commented:
I think you might have to use decimal.parse instead of float.parse...

I think since it's in percentage u don't need to divide by 100...

if at all u need

if (b)
        {
.. Divide by 100 here

Author

Commented:
That will work.  Thank you!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial