Solved

gridview change cell backcolor based on variable

Posted on 2008-10-26
6
598 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
[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
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

751 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