Solved

gridview change cell backcolor based on variable

Posted on 2008-10-26
6
593 Views
Last Modified: 2013-12-17
I have a gridview in C# (2005/.Net2.0) and I would like to change the bg color of a cell (or the text color I really don't care at this point) based on the value of a variable.

If the cell falls in the 6th column, called "Age", and the value of a particular cell is above the average age ( double varAvgAge) then I would like the bg color to be Red. If the value of a cell is less than the average age, I would like the color to be green.

here is the code for my gridview:

        DataSet usersDataSet;
        string filePath = Server.MapPath("xml/averages.xml");
        usersDataSet = new DataSet();
        //Read the contents of the XML file into the DataSet
        usersDataSet.ReadXml(filePath);
        GridView1.DataSource = usersDataSet.Tables[0].DefaultView;
        GridView1.DataBind();

thanks in advance
0
Comment
Question by:mackoner
  • 4
  • 2
6 Comments
 
LVL 13

Accepted Solution

by:
sm394 earned 500 total points
ID: 22809227
something like this
 
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int age = Convert.ToInt16(DataBinder.Eval(e.Row.DataItem, "Age"));
       
            if (age == 1)
            {
                e.Row.Cells[6].BackColor = System.Drawing.Color.Aqua;
            }
        }
    }
0
 

Author Comment

by:mackoner
ID: 22809502
thanks for the help.

I still couldn't quite get the color to change based on my variable varAvgAge. varAvgAge is a double, and is assigned a value from a calculation derived from data from the xml file.

I have my gridview set up on pageload
DataSet usersDataSet;
        string filePath = Server.MapPath("xml/average.xml");
        usersDataSet = new DataSet();
        //Read the contents of the XML file into the DataSet
        usersDataSet.ReadXml(filePath);
        GridView1.DataSource = usersDataSet.Tables[0].DefaultView;
        GridView1.DataBind();
0
 

Author Comment

by:mackoner
ID: 22810117
i am not sure if I have to loop thru each record to find it's value for age and THEN do some sort of compairison??
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:mackoner
ID: 22810418
Also, a bit of sorting going on in the GridView:

            DataSet usersDataSet;
            string filePath = Server.MapPath("xml/average.xml");
            usersDataSet = new DataSet();
            //Read the contents of the XML file into the DataSet
            usersDataSet.ReadXml(filePath);
           
            DataView dv = usersDataSet.Tables[0].DefaultView;
            dv.Sort = "company, name, age ASC";
     
            GridView1.DataSource = dv;
            GridView1.DataBind();
0
 

Author Closing Comment

by:mackoner
ID: 31510151
I forgot to add "GridView1_RowCreated" to the source
0
 
LVL 13

Expert Comment

by:sm394
ID: 22810847
debug i.e put breakpoint on this line
 int age = Convert.ToInt16(DataBinder.Eval(e.Row.DataItem, "Age"));
and check what value you getting in age
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Free Alternative to JIRA 4 58
Visual Studio installation 5 39
Code enhancement 5 14
Achieve json result 2 35
Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now