?
Solved

How do I compare a GridView cell value with a float?

Posted on 2009-07-03
4
Medium Priority
?
618 Views
Last Modified: 2012-05-07
I am using an asp:GridView to display data from SQL Server.  I would like to change the color of a cell by comparing a float to the value of the cell (TableCell).  I am using c#.  Below is a snippet that shows what I am attempting to accomplish.  Thank you in advance.
if ((e.Row.Cells[1].Text != "Text on header row") && (e.Rows.Cells[1] < 0.65))
            {
                e.Row.Cells[1].BackColor = System.Drawing.ColorTranslator.FromHtml("#000FF");
            }

Open in new window

0
Comment
Question by:Budrophious
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24774767
If the cell is certain to have a double/float value, you can use:


if ((e.Row.Cells[1].Text != "Text on header row") && (Double.Parse(e.Rows.Cells[1].Text) < 0.65))
{
    e.Row.Cells[1].BackColor = System.Drawing.ColorTranslator.FromHtml("#000FF");
}

Open in new window

0
 
LVL 39

Accepted Solution

by:
abel earned 1000 total points
ID: 24774772
(in the above code Double should be double)

If it is sometimes valid and sometimes not, change it like this:

double cellValue;
double.TryParse(e.Rows.Cells[1].Text, out cellValue);
// cellValue will contain 0.0 if it is not convertible
if ((e.Row.Cells[1].Text != "Text on header row") && (cellValue < 0.65))
{
    e.Row.Cells[1].BackColor = System.Drawing.ColorTranslator.FromHtml("#000FF");
}

Open in new window

0
 
LVL 15

Assisted Solution

by:jinal
jinal earned 1000 total points
ID: 24775110
Hello ,

I attached code in code snippet. Mailny you have to handle GridView RowDataBound Event .

No need to check headertext and just need to check rowtype.

This is a main part.

    protected void GridView1_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.65f)
                {
                    e.Row.Cells[1].BackColor = System.Drawing.ColorTranslator.FromHtml("#000FF");
                }
            }
        }
    }





/* ASPX File */
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" 
            onrowdatabound="GridView1_RowDataBound">
        </asp:GridView>    
    </div>
    </form>
</body>
</html>
 
/* Code Behind File */
 
public partial class Default6 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Value", typeof(float));
 
            DataRow dr = dt.NewRow();
            dr[0] = "Test1";
            dr[1] = 0.5f;
            dt.Rows.Add(dr);
 
            dr = dt.NewRow();
            dr[0] = "Test2";
            dr[1] = 1.5f;
            dt.Rows.Add(dr);
 
            GridView1.DataSource = dt;
            GridView1.DataBind();
 
        }
    }
    protected void GridView1_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.65f)
                {
                    e.Row.Cells[1].BackColor = System.Drawing.ColorTranslator.FromHtml("#000FF");
                }
            }
        }
    }
}

Open in new window

0
 

Author Closing Comment

by:Budrophious
ID: 31599693
Thank you for your responses!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question