Budrophious
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;
*/
}
}
}
ASKER
I have not. Which line are you referring to?
if (cellvalue >= 0.90)
ASKER
I made a correction (I was assigning to b twice).
cellvalue is 0.0.
The if statement is not being entered.
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;
*/
}
}
}
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...
float.TryParse(e.Row.Cells
e.Row.Cells[1].Text is returning non numeric.. Can u confirm that...
ASKER
The value of e.Row.Cells[1].Text is "90.37 %".
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
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
}
}
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That will work. Thank you!
cellvalue : Line 19